UIPanGestureRecognizer的跟随手势

跟随效果图

UIPanGestureRecognizer的跟随手势_第1张图片
随手势移动

不跟随效果图

UIPanGestureRecognizer的跟随手势_第2张图片
一下就跑丢了

注意需要调用一下setTranslation: inView:方法

#import "PanViewController.h"

@interface PanViewController()
{
    UIView *_testView;
}

@end


@implementation PanViewController

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        self.navigationItem.title = @"Pan";
        self.edgesForExtendedLayout = UIRectEdgeNone;
        
        _testView = [[UIView alloc] init];
        _testView.backgroundColor = [UIColor orangeColor];
        
        UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
        [_testView addGestureRecognizer:panGesture];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    _testView.frame = CGRectMake(50, 50, 150, 150);
    [self.view addSubview:_testView];
}

- (void)pan:(UIPanGestureRecognizer *)gesture
{
    CGPoint translatePoint = [gesture translationInView:self.view];
    _testView.center = CGPointMake(_testView.center.x + translatePoint.x, _testView.center.y + translatePoint.y);
    [gesture setTranslation:CGPointZero inView:self.view];
    
    if (gesture.state == UIGestureRecognizerStatePossible)
    {
        NSLog(@"possible");
    }
    else if (gesture.state == UIGestureRecognizerStateBegan)
    {
        NSLog(@"began");
    }
    else if (gesture.state == UIGestureRecognizerStateChanged)
    {
        NSLog(@"changed");
    }
    else if (gesture.state == UIGestureRecognizerStateEnded)
    {
        NSLog(@"ended");
    }
}

@end

你可能感兴趣的:(UIPanGestureRecognizer的跟随手势)