UI07_Touch

创建一个根视图控制器

根视图控制器

@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类,继承于UIView
MyView.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和两个UIButton
RootViewController.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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值