IOS UIEvent小结

在IOS开发中我们通常会用到手势  但UIEvent 也必不可少 相比手势 更加直接


/*
  uievent 是由设备部或对硬件的操作 每个事件都是一个uievent 对象
  ios的事件有三种 触摸事件 摇晃事件  远程控制事件
  触摸事件是由用户 都屏幕触摸产生的事件 对于 UIView或者其子类可以接收到触摸事件 只是没有作出响应
  如果想要作出相响应 就要实现以下方法
  
  */
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    //当手指触摸屏幕时(刚开始接触)
//    [self modifiySuperviewColor];
//    [self modifySelfviewColor];
    //touches存储触摸屏幕的手指对象,每一个手指对象都是一个UITouch的对象
    NSLog(@"%lu",[touches count]);
    //随机色
    //单机改变自身颜色
//    1,获取手指对象
    UITouch *touch1 = [touches anyObject];
//    2,获取手指点击次数
    if ([touch1 tapCount] == 2) {
        //取消之前的任务
        [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(modifySelfviewColor) object:nil];
        [self performSelector:@selector(modifiySuperviewColor) withObject:nil afterDelay:0.3];
    }else if([touch1 tapCount] == 1){
        [self performSelector:@selector(modifySelfviewColor) withObject:nil afterDelay:0.3];
    }else if([touch1 tapCount] == 3){
        [self modifySelfviewLocation];
        [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(modifiySuperviewColor) object:nil];
            }
    NSLog(@"%s",__FUNCTION__);
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
    //当触摸被取消时 (前提是手指之前在触摸屏幕)
//    例如当电话进入时
    NSLog(@"%s",__FUNCTION__);
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

    //当手指离开屏幕时 (触摸结束)
//    [self modifySelfviewLocation];

     NSLog(@"%s",__FUNCTION__);
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
        //当是一直触摸屏幕并且在视图内移动时

     NSLog(@"%s",__FUNCTION__);
}


//修改自身颜色

-(void)modifySelfviewColor{
    self.backgroundColor = [UIColor randomColor];
}

//修改父视图颜色

-(void)modifiySuperviewColor{
    self.superview.backgroundColor = [UIColor randomColor];
}

//改变自身位置
-(void)modifySelfviewLocation{
    }


#import "UIColor+RandomColor.h"

@implementation UIColor (RandomColor)
+(UIColor *)randomColor{
    return  [self colorWithRed:arc4random() % 256 /255.0 green:arc4random() % 256 /255.0 blue:arc4random() % 256 /255.0 alpha:1.0];
}
@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值