码迷,mamicode.com
首页 > 其他好文 > 详细

手势 - 单击、长按、拖动等

时间:2015-11-09 18:34:15      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:

@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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!