标签:
@implementation ViewController
UILabel *labelView;
int startX;
int startY;
- (void)viewDidLoad {
[super viewDidLoad];
startX = 100;
startY = 100;
labelView = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 50)];
labelView.backgroundColor = [UIColor redColor];
labelView.text = @"click text";
labelView.textColor = [UIColor whiteColor];
labelView.font = [UIFont systemFontOfSize:15];
labelView.contentMode = UIViewContentModeCenter;
labelView.textAlignment = kCTTextAlignmentCenter;
labelView.userInteractionEnabled = YES;
// 单击
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleClick:)];
[labelView addGestureRecognizer:tapGesture];
// 滑动事件
UISwipeGestureRecognizer *swapGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGestureHandle:)];
[self.view addGestureRecognizer:swapGesture];
// 拖动
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestureRecognizer:)];
[labelView addGestureRecognizer:panGesture];
// 长按
UILongPressGestureRecognizer *longPressGuesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
[labelView addGestureRecognizer:longPressGuesture];
[self.view addSubview:labelView];
}
-(void)singleClick:(UITapGestureRecognizer *)gesture
{
NSLog(@"CLICK");
}
-(void)longPress:(UILongPressGestureRecognizer *)gesture
{
NSLog(@"LONG CLICK");
}
-(void)swipeGestureHandle:(UISwipeGestureRecognizer *)gesture
{
NSInteger direction = gesture.direction;
NSLog(@"direction is: %i", direction);
switch (direction) {
case UISwipeGestureRecognizerDirectionRight:
NSLog(@"TO LEFT");
CGRect frame = CGRectMake(100, 100, labelView.frame.size.width, labelView.frame.size.height);
labelView.frame = frame; break;
case UISwipeGestureRecognizerDirectionLeft:
NSLog(@"TO RIGHT");
break;
default:
break;
}
}
-(void)panGestureRecognizer:(UIPanGestureRecognizer *)gesture
{
// 位移
CGPoint point = [gesture translationInView:labelView];
NSLog(@"point : x = %g, y=%g",point.x, point.y);
CGRect frame = CGRectMake(startX + point.x, startY + point.y, labelView.frame.size.width, labelView.frame.size.height);
labelView.frame = frame;
NSLog(@"gusture is : %i", gesture.state);
if(gesture.state == UIGestureRecognizerStateEnded)
{
startX = labelView.frame.origin.x;
startY = labelView.frame.origin.y;
}
}
标签:
原文地址:http://www.cnblogs.com/xiangjune/p/4950481.html