1 #import "NJViewController.h" 2 3 @interface NJViewController () 4 @property (weak, nonatomic) IBOutlet UIView *customView; 5 6 @end 7 8 @implementation NJViewController 9 10 - (void)viewDidLoad 11 { 12 [super viewDidLoad]; 13 UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] init]; 14 [self.customView addGestureRecognizer:pan]; 15 16 [pan addTarget:self action:@selector(panView:)]; 17 } 18 19 - (void)panView:(UIPanGestureRecognizer *)pan 20 { 21 // 返回的值是以手指按下的点为原点 22 // 1 2 3 4 5 23 CGPoint point = [pan translationInView:pan.view]; 24 25 NSLog(@"拖拽事件 %@", NSStringFromCGPoint(point)); 26 CGPoint temp = self.customView.center; 27 temp.x += point.x; 28 temp.y += point.y; 29 self.customView.center = temp; 30 31 [pan setTranslation:CGPointZero inView:pan.view]; 32 } 33 34 @end
时间: 2024-10-27 12:28:44