ViewController.m
/*
手势:
UIResponder:是一个响应者(传达者) 用来响应 用户触摸屏幕的某些事件
// 手指开始触摸屏幕调用
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
时间戳
点击次数
可以获得点击视图的位置*******
- (CGPoint)locationInView:(nullable UIView *)view;
// 手指触摸屏幕开始移动
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
// 手指离开屏幕的时候调用
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
// 手指因外部事件 取消触摸的时候调用
- (void)touchesCancelled:(nullable NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
手势不调用 检查时候设置了背景颜色
手势又分为六大手势:
六大手势 全部都继承自 UIGestureRecognizer
1、点击 UITapGestureRecognizer
2、长按 UILongPressGestureRecognizer
3、拖拽 UIPanGestureRecognizer
4、捏合 UIPinchGestureRecognizer
5、轻扫 UISwipeGestureRecognizer
6、旋转 UIRotationGestureRecognizer
touchesBegan moved end(鼠标开始、移动、结束)
可以通过touches 获得某个触摸事件
UITouch *touch = [touches anyObjects];
可以通过UITouch 获得触摸的点的位置
*/
#import "ViewController.h"
@interface ViewController ()
{
UIImageView *imageView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
imageView.image = [UIImage imageNamed:@"fly_1"];
[self.view addSubview:imageView];
imageView.animationImages = @[[UIImage imageNamed:@"fly_1"],[UIImage imageNamed:@"fly_2"],[UIImage imageNamed:@"fly_3"],[UIImage imageNamed:@"fly_4"]];
imageView.animationDuration = 1;
//UIGestureRecognizer
//初始化手势
//- (instancetype)initWithTarget:(nullable id)target action:(nullable SEL)action;
//UIView 中有添加手势的方法
//addGestureRecognizer:
//可以通过手势里面的view属性找到点击的视图
//locationInView:找到点击的位置
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(actionOfTap:)];
//设置手势点击的点击次数(才会移动)
tap.numberOfTapsRequired = 2;
//设置点击手指的个数
//tap.numberOfTouchesRequired = 2;
[self.view addGestureRecognizer:tap];
//长按手势
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(actionOfLongPress:)];
//设置长按的时间
longPress.minimumPressDuration = 3;
[self.view addGestureRecognizer:longPress];
}
/*
typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
UIGestureRecognizerStatePossible, 默认状态UIGestureRecognizerStateBegan, 手势开始
UIGestureRecognizerStateChanged, 手势发生改变
UIGestureRecognizerStateEnded, 手势结束
UIGestureRecognizerStateCancelled, 手势被取消状态
UIGestureRecognizerStateFailed, 手势失败的状态
};
*/
//要想获得这些状态可以使用 state ->状态
//点击手势
-(void)actionOfTap:(UITapGestureRecognizer *)sender{
// UIView *view = sender.view;
// view.backgroundColor = [UIColor redColor];
CGPoint point = [sender locationInView:self.view];
// imageView.center = point;(已删除)
//让视图从原始状态到animations里面的状态用2秒执行完
//补间动画(只会让视图随着光标的点击而移动,不会变换他的大小形态)
// [UIView animateWithDuration:2 animations:^{
// imageView.center = point;
// }];
//不仅可以让视图随着光标的移动而移动,也可以在移动的途中和移动结束之后变换形态大小
[UIView animateWithDuration:2 animations:^{
imageView.center = point;
imageView.bounds = CGRectMake(0, 0, 200, 200);
} completion:^(BOOL finished) {
//动画完成之后调用
[UIView animateWithDuration:2 animations:^{
imageView.bounds = CGRectMake(0, 0, 100, 100);
}];//会从原始默认状态 到animations里面状态 用2秒钟执行完
}];
/*
UIView动画
系统帮咱们封装了 核心动画
//设置动画的持续时间(Duratation)和动画
[UIView animateWithDuration:1 animations:^{
}];
//completion 动画完成之后调用的方法
[UIView animateWithDuration:1 animations:^{
} completion:^(BOOL finished) {
}];
*/
}
//长按实现方法
-(void)actionOfLongPress:(UILongPressGestureRecognizer *)sender{
if (sender.state == UIGestureRecognizerStateBegan) {
imageView.bounds = CGRectMake(0, 0, 200, 200);
NSLog(@"放大了~");
}
if (sender.state == UIGestureRecognizerStateEnded) {
imageView.bounds = CGRectMake(0, 0, 100, 100);
NSLog(@"还原了~");
}
}
//- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)even
//{
// //locationInView:UITouch找到点击视图的某一个点
// //anyObject 可以获得touches集合里面某一个touch事件
// UITouch *touch = [touches anyObject];
// //找到触摸视图上面的触摸点
// CGPoint point = [touch locationInView:self.view];
// NSLog(@"x:%f y:%f",point.x,point.y);
//
// //让图片随着鼠标光标的移动而移动
// imageView.center = point;
//
// [imageView startAnimating];
//
//}
//
//// 手指触摸屏幕开始移动
//- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event
//{
// UITouch *touch = [touches anyObject];
// CGPoint point = [touch locationInView:self.view];
// imageView.center = point;
//
//
//}
//// 手指离开屏幕的时候调用
//- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event
//{
//
// [imageView stopAnimating];
//}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
/*
用户点击屏幕触发 -> 所有视图都继承UIResponder->UITuchBegin moved end
UITuchBegin moved end
1、获得点击的touch事件
UITouch *touch = [touches anyObject]
2、获得用户点击的位置
CGPoint point = [touch locationInView:XX];
UIGestureRecognizer:
所有手势都是通过 初始化
- (instancetype)initWithTarget:(nullable id)target action:(nullable SEL)action
把手势添加到视图上
[xx视图 addGestureRecognizer:xx手势];
可以通过手势 获得触摸的视图
view这个属性获得
获得点击视图的位置
CGPoint point = [xx手势 locationInView:xx视图];
1、点击手势
UITapGestureRecognizer
numberOfTapsRequired: 设置点击的次数
numberOfTouchesRequired: 设置点击手指的个数
2、UILongPressGestureRecognizer
minimumPressDuration: 设置长按的最小持续时间 会在长按的持续时间之后执行触发事件
UIView动画
在动画里面 更改视图的属性 产生动画效果
[UIView animateWithDuration:时间 animations:^{
要更改的属性 -> 会产生一个 在设置时间之后 执行完成的一个动画效果
}];
[UIView animateWithDuration:时间 animations:^{
要更改的属性 -> 会产生一个 在设置时间之后 执行完成的一个动画效果
} completion^(BOOL){
当动画执行完毕之后调用
}];
*/
}
@end