如何像UIScrollView一样创建幻灯片

时间:2012-10-29 11:05:36

标签: objective-c ios

我想滑动我的视图,就像元素在UIScrollView中滑动一样。用户可以触摸视图和拉动,并且根据加速度和位置,视图应该离开或停留。

我应该UIPanGestureRecognizer使用UISwipeGestureRecognizer吗?

1 个答案:

答案 0 :(得分:0)

如果您希望内容跟随用户的手指(听起来像您这样做),您想使用 UIPanGestureRecognizer 。滑动手势将等待滑动完成,然后告诉您相关信息。

使用'state'属性获取手指开始时的位置,以及手指完成时的位置,并使用力度来确定加速度。

CGPoint location = [sender locationInView:self.view];

if(sender.state == UIGestureRecognizerStateBegan)
{
         startLocation = location;
}
  else if(sender.state == UIGestureRecognizerStateChanged)
{
      // Move view or do something to give feedback to user while they swipe
}
else if(sender.state == UIGestureRecognizerStateEnded)
{
    CGPoint distanceMoved = CGPointMake(location.x - startLocation.x, location.y - startLocation.y);
    CGPoint velocity = [sender velocityInView:self.view];
 // Logic goes here
}