手势 UIPanGestureRecognizer 平移手势
更新:HHH   时间:2023-1-7





UIPanGestureRecognizer * panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGesture:)];

   [redView addGestureRecognizer:panGesture];

   [panGesture release];




- (void)panGesture:(UIPanGestureRecognizer *)panGesture
{
    CGPoint point = [panGesture translationInView:panGesture.view];

    panGesture.view.transform = CGAffineTransformMakeTranslation(point.x, point.y);
    
    panGesture.view.transform = CGAffineTransformTranslate(panGesture.view.transform, point.x, point.y);
    

将之前增量清零
    
    [panGesture setTranslation:point inView:panGesture.view];
    
   

获取的是手指位置的改变量  , 存储在x轴以及y轴的改变量

 CGPoint point = [panGesture translationInView:panGesture.view];
    

 panGesture.view.center = CGPointMake(panGesture.view.center.x+ point.x,panGesture.view.center.y+ point.y);


将之前的增量清零 重置之前的增量

   [panGesture setTranslation:CGPointZero inView:panGesture.view];

 panGesture.view.backgroundColor  = [UIColor random];
}

返回开发技术教程...