@property (nonatomic, strong) UILabel *lbl;
-(void)initLabel {
CGRect labelFrame = CGRectMake(0.0f, 0.0f, 150.0f, 100.0f);
self.lbl = [[UILabel alloc] initWithFrame:labelFrame];
self.lbl.text = @"Hello World";
self.lbl.backgroundColor = [UIColor blackColor];
self.lbl.textColor = [UIColor whiteColor];
self.lbl.textAlignment = NSTextAlignmentCenter;
//与用户交互
self.helloWorldLabel.userInteractionEnabled = YES;
[self.view addSubview:self.lbl];
}
- (void)addGesture {
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGestures:)];
self.panGesture.minimumNumberOfTouches = 1;
self.panGesture.maximumNumberOfTouches = 1;
[self.lbl addGestureRecognizer:self.panGesture];
}
- (void) handlePanGestures:(UIPanGestureRecognizer*)paramSender{
if (paramSender.state !=UIGestureRecognizerStateEnded &&
paramSender.state !=UIGestureRecognizerStateFailed){ //开始状态:UIGestureRecognizerStateBegan
CGPoint location = [paramSender locationInView:paramSender.view.superview];
paramSender.view.center = location; //paramSender.view : UILabel 因为原先是UILabel addGestureRecognizer
}
}
postscript: swipe:滑动,快速移动 pan:拖移,慢速移动