zoukankan      html  css  js  c++  java
  • IOS开发中长按的手势事件编程

    长按手势事件:

    长按按钮1S后改变按钮颜色:

     1 //  长按事件
     2 #import "ViewController.h"
     3 @interface ViewController (){
     4     UIButton *myBtn;
     5 }
     6 @end
     7 @implementation ViewController
     8 - (void)viewDidLoad {
     9     [super viewDidLoad];
    10     myBtn = [[UIButton alloc]initWithFrame:CGRectMake(100, 200, 214, 80)];
    11     myBtn.backgroundColor = [UIColor orangeColor];
    12     [myBtn setTitle:@"开始按钮" forState:UIControlStateNormal];
    13     UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];//初始化一个长按手势
    14     [longPress setMinimumPressDuration:1];//设置按多久之后触发事件
    15     [myBtn addGestureRecognizer:longPress];//把长按手势添加给按钮
    16     [self.view addSubview:myBtn];
    17 }
    18 -(void)longPressAction:(UILongPressGestureRecognizer*)sender{
    19 //    UIGestureRecognizerStatePossible,按钮state的各种枚举值
    20 //    UIGestureRecognizerStateBegan,
    21 //    UIGestureRecognizerStateChanged,
    22 //    UIGestureRecognizerStateEnded,
    23 //    UIGestureRecognizerStateCancelled,
    24 //    UIGestureRecognizerStateFailed,
    25 //    UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded
    26     if (sender.state == UIGestureRecognizerStateBegan) {
    27          myBtn.backgroundColor = [UIColor greenColor];//当状态为Began时,触发事件(修改btn的背景色)
    28     }
    29 }
    30 @end
  • 相关阅读:
    JS中的getter与setter
    Node.js中exports与module.exports的区别
    JS中的匿名函数自执行、函数声明与函数表达式
    JS实现千分位
    JS中的new操作符原理解析
    JS中null与undefined的区别
    JavaScript中callee与caller,apply与call解析
    解决vue路由与锚点冲突
    jQuery中deferred的对象使用
    Vue的生命周期
  • 原文地址:https://www.cnblogs.com/jiwangbujiu/p/5455043.html
Copyright © 2011-2022 走看看