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 --------->摇一摇结束