8-26 UIControl UIGestureRecognizer 控件

这篇博客详细介绍了iOS中的UIControl类及其用于监听事件的功能,特别提到了如何为UIImageView添加监听。此外,还讲解了UIGestureRecognizer的子类,如UITapGestureRecognizer、UILongPressGestureRecognizer等,以及各种手势的相关属性。同时,文中列举了多个常用控件的属性和用法,如UISegmentedControl、UISlider、UIProgressView、UISwitch和UIActivityIndicatorView等。

UIControl,一个交互类

UIImageView无自带的监听事件,可用UIControl来帮助实现监听

UIImageView的交互属性一定要手动打开,默认是关闭的

    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"01"]];
    //交互属性打开
    imageView.userInteractionEnabled = YES;

    [imageView setBounds:CGRectMake(0, 0, 40, 40)];
    [imageView setCenter:CGPointMake(160, 240)];
    [self.view addSubview:imageView];
    
    UIControl *control = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    [control addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    //将control添加到imageView上
    [imageView addSubview:control];


UIGestureRecognizer,手势控制抽象类


UITapGestureRecognizer:点击手势

     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gesture:)];
    //点击的次数
    tap.numberOfTapsRequired = 2;
    //手指数
    tap.numberOfTouchesRequired = 2;
    //视图控件
    //对视图添加点击手势
    [self.view addGestureRecognizer:tap];

UILongPressGestureRecognizer:长按手势

    UILongPressGestureRecognizer *lon = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gesture:)];
    //最短时间
    lon.minimumPressDuration = 3;
    //响应之前可移动的距离
    lon.allowableMovement = 240;
    [self.view addGestureRecognizer:lon];

UIRotationGestureRecognizer:旋转手势


     UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(gesture:)];
    
    [self.view addGestureRecognizer:rotation];


属性:

rotation                 旋转的角度(弧度)

     velocity                 角速度


UISwipeGestureRecognizer:滑动手势


     UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(gesture:)];
    swipe.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:swipe];


属性:</

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值