UI07_Touch

来源:互联网 发布:淘宝客服必须用电脑吗 编辑:程序博客网 时间:2024/05/11 20:50
创建一个根视图控制器

根视图控制器

@implementation RootViewController#warning 在继承和使用父类的方法的时候,一般会先用super去调用父类相应的方法,目的是为了保证方法的原功能不变,在此基础上再添加我们自己的功能.(一).触摸1.触摸开始.- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {    [super touchesBegan:touches withEvent:event];    NSLog(@"触摸开始");}2.触摸移动.- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {    [super touchesMoved:touches withEvent:event];    NSLog(@"触摸移动");}3.触摸结束.- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {    [super touchesEnded:touches withEvent:event];    NSLog(@"触摸结束");}4.触摸取消(当来电话时,触摸自动取消).- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {    [super touchesCancelled:touches withEvent:event];    NSLog(@"触摸取消");}

触摸结果

(二).摇一摇1.晃动开始.- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {    [super motionBegan:motion withEvent:event];    NSLog(@"晃动开始!~");    // 设置随机背景颜色.(模拟器 -> Hardware -> Shake),模拟摇一摇改变背景颜色.    self.view.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];}2.晃动结束.- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {    [super motionEnded:motion withEvent:event];    NSLog(@"晃动结束!~");}3.晃动取消.- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {    [super motionCancelled:motion withEvent:event];    NSLog(@"晃动取消!~");}

模拟晃动结果

(三).在根视图控制器上移动视图.新建MyView类,继承于UIViewMyView.m#import "MyView.h"@interface MyView ()1.创建一个属性,记录起点.@property(nonatomic, assign)CGPoint startPoint;@end@implementation MyView// 开始- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {    [super touchesBegan:touches withEvent:event];    2.获取触摸对象.    UITouch *touch = [touches anyObject];    3.根据触摸对象,获取开始的坐标位置.    self.startPoint = [touch locationInView:self];}// 移动- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {    [super touchesMoved:touches withEvent:event];    4.获取触摸对象.    UITouch *touch = [touches anyObject];    5.获取移动的坐标位置.    CGPoint newPoint = [touch locationInView:self];    6.计算两个坐标产生的变化.    CGFloat dx = newPoint.x - self.startPoint.x;    CGFloat dy = newPoint.y - self.startPoint.y;    7.改变自身变化.    self.center = CGPointMake(self.center.x + dx, self.center.y + dy);}// 结束- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {    [super touchesEnded:touches withEvent:event];}// 回到RootViewController中创建一个视图.RootViewController.m- (void)viewDidLoad {    // 为什么要单独创建一个类写方法的实现,然后再回到根视图控制器中创建视图,因为代码模块化,便于管理.    MyView *firstView = [[MyView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];    firstView.backgroundColor = [UIColor cyanColor];    [self.view addSubview:firstView];    [firstView release];}

移动过程

(四).响应链问题在根视图控制器中创建一个UIView和两个UIButtonRootViewController.m- (void)viewDidLoad {    1.创建一个视图.    UIView *firstView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];    firstView.backgroundColor = [UIColor cyanColor];    [self.view addSubview:firstView];    [firstView release];    2.创建第一个按钮.    UIButton *firstButton = [UIButton buttonWithType:UIButtonTypeSystem];    firstButton.frame = CGRectMake(0, 0, 100, 100);    firstButton.backgroundColor = [UIColor redColor];    [firstView addSubview:firstButton];    [firstButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];    3.创建第二个按钮.    UIButton *secondButton = [UIButton buttonWithType:UIButtonTypeSystem];    secondButton.frame = CGRectMake(150, 150, 100, 100);    secondButton.backgroundColor = [UIColor blueColor];    [firstView addSubview:secondButton];    [secondButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];}4.使这两个按钮同时绑定同一个方法.- (void)click:(UIButton *)button {    NSLog(@"****************");}我们可以发现当鼠标点击按钮与视图的重合区域时,会触发click方法,其他不会触发,这就是响应链的问题.

这里写图片描述

0 0
原创粉丝点击