IOS 拖拽事件(手势识别)

 

@interface NJViewController ()
@property (weak, nonatomic) IBOutlet UIView *customView;

@end

@implementation NJViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIPanGestureRecognizer  *pan = [[UIPanGestureRecognizer alloc] init];
    [self.customView addGestureRecognizer:pan];
    
    [pan addTarget:self action:@selector(panView:)];
}

- (void)panView:(UIPanGestureRecognizer *)pan
{
    // 返回的值是以手指按下的点为原点
    // 1 2 3 4 5
    CGPoint point = [pan translationInView:pan.view];
    
    NSLog(@"拖拽事件 %@", NSStringFromCGPoint(point));
    CGPoint temp = self.customView.center;
    temp.x += point.x;
    temp.y += point.y;
    self.customView.center = temp;
    
    // 理解不了就记住就OK
    [pan setTranslation:CGPointZero inView:pan.view];
}

 

posted on 2017-03-22 12:56  守望星空  阅读(243)  评论(0编辑  收藏  举报

导航