• 手势


    @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 *leftSwapGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGestureHandle:)];

        [leftSwapGesture setDirection:UISwipeGestureRecognizerDirectionLeft];

        UISwipeGestureRecognizer *rightSwapGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGestureHandle:)];

        [rightSwapGesture setDirection:UISwipeGestureRecognizerDirectionRight];

        [self.view addGestureRecognizer:leftSwapGesture];

        [self.view addGestureRecognizer:rightSwapGesture];

        // 拖动

        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;

        }

    }

  • 相关阅读:
    基于HTML5的多张图片上传
    如何限制textarea文本框的输入字数
    页面第一次加载实现图片淡入方式加载
    ajax实现的无刷新分页代码实例
    26个Jquery使用小技巧
    Windows下搭建PHP开发环境
    Jquery插件之ajaxForm ajaxSubmit的理解用法
    Java XML解析器
    JS截取字符串
    在Eclipse中配置tomcat
  • 原文地址:https://www.cnblogs.com/xiangjune/p/4950481.html
Copyright © 2020-2023  润新知