UI基础之----触摸事件

来源:互联网 发布:东研网络 同花顺 编辑:程序博客网 时间:2024/06/05 15:22

一 在创建按钮的时候经常使用颜色类,所以可以添加颜色类的方法,生成随机颜色

1.  首先UIColor 类添加一个类目,添加生成随机颜色的方法

2.  定义宏,用来计算 GRB 的值

#defineCOLORVALUE arc4random()%256/255.0

+(UIColor *)randomColor{

return[UIColorcolorWithRed: COLORVALUE green: COLORVALUE blue: COLORVALUE alpha:1.0];

}

二 触摸事件的方法

1.触摸事件

1⃣一根或多根手指开始触摸 view, 系统会自动调用 view 下面的方法:

-  (void)touchesBegan:(NSSet *)touches withEvent: (UIEvent *)event;

//触摸开始

2⃣  一根或多根手指在 view 上移动,系统会自动调用触摸移动方法:

-  (void)touchesMoved:(NSSet *) touches withEvent: (UIEvent *)event;

3⃣ 一根或者多跟手指离开 view, 系统会调用触摸结束方法:

-  (void)touchesEnded:(NSSet *) touches withEvent: (UIEvent *)event;

4⃣触摸结束前,摸个系统事件(例如电话呼入)会打断触摸过程,系统会自动调动 view 下面的方法

-  (void)touchesCancelled:(NSSet *) touches withEvent: (UIEvent *)event;

2.如果两个手指同时触摸一个 view, 那么 view 只会调用一次 touchesBegan:withEvent: 方法, touches 参数中装着两个 UITouch 对象.

3.触摸事件的属性

4.  触摸事件的方法

5.  根据点击 view 的次数判断来改变屏幕背景的颜色

#import "TouchView.h"#import "UIColor+Random.h"@implementation TouchView// 如果想让一个视图对触摸事件作出回应,需要实现下面的方法// 触摸开始,刚开始触摸到视图的时候触发- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    // UITouch 点击类    // touches 存放手指对象    UITouch *touch = [touches anyObject];    if (1 == touch.tapCount) {    // 记录点击的次数        // 当视图识别为单击操作时,延迟执行下面的方法,看是否会有双击发生        //self.backgroundColor = [UIColor randomColor];        [self performSelector:@selector(changeMyselfBackColor) withObject:nil afterDelay:0.3];    }else if( 2 == touch.tapCount){        // 当识别为双击的时候,取消之前的操作        [ NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(changeMyselfBackColor) object:nil];        self.superview.backgroundColor = [UIColor randomColor];    }    //  调用改变中心点的方法    [self changeMyselfCenter];    NSLog(@"%s",__func__);}- (void)changeMyselfBackColor{    // 改变视图的颜色    self.backgroundColor = [UIColor randomColor];}// 改变中心点的方法- (void)changeMyselfCenter{    self.center = CGPointMake(arc4random()%101 + 100, arc4random()%200 + 120);}// 触摸取消,事件意外中断的时候触发(比如来电话了)- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{    NSLog(@"%s",__func__);}// 触摸结束,当手指离开视图屏幕的时候触发- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{    NSLog(@"%s %d",__func__,__LINE__);}// 触摸移动,当我们的手指在视图上移动的时候触发- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{    NSLog(@"%s %d",__func__,__LINE__);}
6.当鼠标在 view 上移动的时候,移动创建的 view 视图

#import "MoveView.h"@implementation MoveView  // 触摸事件的方法没必要全部实现- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{   // 1.获取手指对象    UITouch *touch = [touches anyObject];    // 2.获取自身视图上的,当前的手指位置    CGPoint point = [touch locationInView:self];    // 3.获取手指之前的位置    CGPoint point1 = [touch previousLocationInView:self];    // 4.计算移动增量的 x 值和y 值    CGFloat dx = point.x - point1.x;    CGFloat dy = point.y - point1.y;    // 5.获取中心位置    CGPoint center = self.center;    self.center = CGPointMake(center.x + dx, center.y + dy);}@end

7.  屏幕捏合事件

#import "PinchView.h"@implementation PinchView- (instancetype)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {    // iOS支持多点触摸,但是默认只能单点触摸.        self.multipleTouchEnabled = YES;    }    return self;}- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{    if (1 == touches.count) {        return;    }else{        // 1.取出手指对象所在的集合        NSArray  *allTouch = [touches allObjects];        // 2.获取手指对象        UITouch *touch1 = [allTouch firstObject];        UITouch *touch2 = [allTouch lastObject];        // 3.获取手指在视图中的位置        CGPoint currentFirstPoint = [touch1 locationInView:self];        CGPoint currentSecondPoint = [touch2 locationInView:self];        // 4.获取当前两点之间的距离        CGFloat currentDistance = [self distanFromPoint:currentFirstPoint toPoint:currentSecondPoint];        // 5.获取手指在视图上之前的位置        CGPoint previousFirstPoint = [touch1 previousLocationInView:self];        CGPoint previousSecondPoint = [touch2 previousLocationInView:self];        // 6.获取之前两点之间的距离        CGFloat previousDistance = [self distanFromPoint:previousFirstPoint toPoint:previousSecondPoint];        // 7.计算捏合前后的比例        CGFloat rate = currentDistance / previousDistance;        // 8.缩放视图,如果视图的大小变化,中心点不变,修改 bounds 即可        self.bounds = CGRectMake(0, 0, self.frame.size.width * rate, self.frame.size.height * rate);    }}// 计算两个点之间的距离方法- (CGFloat)distanFromPoint:(CGPoint)fromPoint toPoint:(CGPoint)toPoint{    CGFloat dx = fromPoint.x - toPoint.x;    CGFloat dy = fromPoint.y - toPoint.y;    return sqrt(dx*dx + dy*dy);}@end


0 0
原创粉丝点击