UITouch

Touch的方法

- (void)touchesBegan ---->触摸开始(点击空白处回收键盘 , 其实就是让textField放弃第一响应者)

<span style="font-size:18px;">- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    NSLog(@"触摸开始");
    UITextField *text = (UITextField *)[self.view viewWithTag:1000];
    // 点击空白处回收键盘
    [text resignFirstResponder];
}
</span>

- (void)touchesMoved ------>正在触摸

- (void)touchesEnded ------->触摸结束

- (void)touchesCancelled ------->触摸中断

通过touch方法,让图片跟着手指移动(坐标移动)

1.在触摸开始的方法里,先寻找触摸的对象,通过触摸对象再找到触摸的起始位置,用属性point保存(先定义一个cgpoint point)

2.先找到UITouch对象,集合里只有一个触摸对象(集合)

3.通过touch对象找在view上的位置   self代表MyView  self.point接受结果

<span style="font-size:18px;">- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    // 1.现在触摸的方法里,先寻找触摸的对象,通过触摸对象再找到触摸的起始位置,用属性point保存
    // 2.先找到UITouch对象,集合里只有一个触摸对象(集合)
    UITouch *touch = [touches anyObject];
    // 3.通过touch对象找在view上的位置  self代表MyView  用self.point接收结果
    self.point = [touch locationInView:self];
    
}</span>

1.在正在触摸的方法里,计算坐标.首先获取touch对象  UITouch *touch = [touches anyObject];

2.获取移动的坐标   CGPoint newPoint  = [touch locationInView:self];

3.跟原始的坐标位置进行比较 

4.根据变化重新设置MyView的尺寸 

<span style="font-size:18px;">- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesMoved:touches withEvent:event];
    // 1.先获取touch对象
    UITouch *touch = [touches anyObject];
    // 2.获取移动的坐标
    CGPoint newPoint = [touch locationInView:self];
    // 3.跟原始的坐标位置进行比较
    CGFloat x = newPoint.x - self.point.x;
    CGFloat y = newPoint.y - self.point.y;
    // 4.根据变化重新设置MyView的尺寸
    self.center = CGPointMake(self.center.x + x, self.center.y + y);
    
}</span>

motion的三种方法(摇一摇)

- (void)motionBegan ------->摇一摇开始

- (void)motionCancelled ------>摇一摇取消

- (void)motionEnded --------->摇一摇结束


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值