import "AppDelegate.h"
@interface AppDelegate ()
@property (nonatomic ,retain)UIImageView *imageView;
@end
@implementation AppDelegate
// 自定义按钮的回调
-(void)customBtn:(UIButton*)sender{
NSLog(@"************");
}
-
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[self.window setRootViewController:[[UIViewController alloc]init]];// 自定义按钮
CustomButton *customBtn = [[CustomButton alloc]initWithFrame:CGRectMake(50, 30, 80, 50)];
[customBtn setTitle:@"我是假冒的"];
[customBtn addTarget:self action:@selector(customBtn:) forControlEvents:customButtonTouchUpInside];
[self.window addSubview:customBtn];// 初始化一个视图(响应者)来承载手势
UIView *gestureView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
// 当前视图放置在屏幕中
gestureView.center = self.window.center;
gestureView.backgroundColor = [UIColor redColor];
[self.window addSubview:gestureView];// 初始化textField(回收键盘所用)
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(150, 150, 100, 50)];
textField.placeholder = @"请输入。。。。";
textField.tag = 1000;
[self.window addSubview:textField];// 初始化UIImageView
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(150, 200, 150, 200)];
imageView.tag = 3000;// imageView动图
NSMutableArray *mArray = [NSMutableArray array];
for (int i = 1 ; i < 12 ; i++) {
[mArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%d.tiff",i]]];
}
imageView.animationImages = mArray;
// 设置动画执行的时长(单位为秒)
imageView.animationDuration = 1.5;
// 开始动画
[imageView startAnimating];
// 动画结束
[imageView stopAnimating];imageView.image = [UIImage imageNamed:@"11.png"];
// UIImageView 的用户交互默认是关闭的,要想使他处理触摸事件,我们需要手动打开它
imageView.userInteractionEnabled = YES;
[self.window addSubview:imageView];// 创建开启、结束动画的按钮
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = CGRectMake(150, 0, 80, 50);
[btn setTitle:@"开启/停止" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:btn];// 轻拍手势
UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];// 设置触控对象(触控点个数)
[tapGR setNumberOfTouchesRequired:1];
// 设置轻拍的次数(点击次数)
[tapGR setNumberOfTapsRequired:2];
// 给创建好的视图添加手势(一个视图可以添加多个手势,但是一个手势只能添加到一个视图上面)
// [gestureView addGestureRecognizer:tapGR];
// 点击屏幕空白处回收键盘(取消textField的第一响应者)
// [self.window addGestureRecognizer:tapGR]; // 回收键盘所用
// 给图片添加手势
[imageView addGestureRecognizer:tapGR];// 捏合手势
UIPinchGestureRecognizer *pinchGR = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
[imageView addGestureRecognizer:pinchGR];
pinchGR.delegate = self;// 旋转手势
UIRotationGestureRecognizer *rotationGR = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
[imageView addGestureRecognizer:rotationGR];// 平移手势
// UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
// [imageView addGestureRecognizer:panGR];
// 屏幕边缘轻扫手势
UIScreenEdgePanGestureRecognizer *screenEdgePanGR = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(screenEdgePanAction:)];
[self.window addGestureRecognizer:screenEdgePanGR];
// 长按手势
UILongPressGestureRecognizer *longPressGR = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
[self.window addGestureRecognizer:longPressGR];
// 轻扫手势
UISwipeGestureRecognizer *swipeGR = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
[self.window addGestureRecognizer:swipeGR];
return YES;
};
pragma mark -- 手势的代理方法
// 使得多个手势可以同时相应
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
// 返回值为YES时,当执行一个手势的操作的时候,也可以执行其他手势的操作
return YES;
}
// 轻拍手势的回调方法
-(void)tapAction:(UITapGestureRecognizer*)sender{
// 可以根据手势得到它当前所作用的视图
// UIImageView imageView = (UIImageView)sender.view;
// imageView.frame = CGRectMake(50, 50, 50, 50);
NSLog(@"我轻拍了gestureView");
// // 得到textField(注意类型强转)
// UITextField tagTextField = (UITextField)[self.window viewWithTag:1000];
// // 回收键盘 (取消第一响应者)
// [tagTextField resignFirstResponder];
}
// 捏合手势的回调方法
-(void)pinchAction:(UIPinchGestureRecognizer*)sender{
// 通过捏合手势得到缩放比例
float scale = sender.scale;
NSLog(@"%.1f",scale);
// 得到该手势作用的视图
UIView *view = sender.view;
// 2D仿射变换函数中 的缩放函数来实现视图的放大缩小
// 是在原有基础上改变当前视图
// <#CGAffineTransform t#>参数:现有视图的transform值
// CGFloat sx 参数:x轴上的缩放比例
// <#CGFloat sy#>参数:y轴上的缩放比例
// view.transform = CGAffineTransformMakeScale(0.5, 0.5); // 在视图最初(初始状态)的transform状态上改变,不管执行多少次,都是以最初的transform状态为基础来改变的
view.transform = CGAffineTransformScale(view.transform, scale, scale);
// 每次捏合动作完毕之后,上次捏合值复原,使它每次都是从100%开始缩放
sender.scale = 1;
NSLog(@"捏合手势");
}
// 旋转手势的回调方法
-(void)rotationAction:(UIRotationGestureRecognizer*)sender{
// 通过该手势得到旋转角度
float rotation = sender.rotation;
// 得到该手势作用的视图
UIView *view = sender.view;
// 通过2D仿射变换函数中的旋转函数来使得当前视图旋转
// <#CGAffineTransform t#>参数:现有视图的transform值(矩阵)
// <#CGFloat angle#>参数:旋转角度
view.transform = CGAffineTransformRotate(view.transform, rotation);
// 旋转之后复原
sender.rotation = 0;
}
// 平移手势的回调方法
-(void)panAction:(UIPanGestureRecognizer*)sender{
// 得到该手势作用的视图
UIView *view = sender.view;
// 得到我们在视图上移动的偏移量
CGPoint currentPoint = [sender translationInView:view.superview];
// 通过2D仿射变换函数中与位移有关的函数实现视图位置变化
view.transform = CGAffineTransformTranslate(view.transform, currentPoint.x , currentPoint.y);
// 复原
[sender setTranslation:CGPointZero inView:view.superview];
NSLog(@"平移手势");
}
// 屏幕边缘轻扫手势回调方法
-(void)screenEdgePanAction:(UIScreenEdgePanGestureRecognizer*)sender{
NSLog(@"屏幕边缘轻扫手势");
}
// 长按手势的回调方法
-(void)longPressAction:(UILongPressGestureRecognizer*)sender{
// 设置当前长按最小的时长
sender.minimumPressDuration = 1.5;
// 设置允许移动的范围
sender.allowableMovement = 2;
NSLog(@"长按手势");
}
// 轻扫手势
-(void)swipeAction:(UISwipeGestureRecognizer*)sender{
sender.numberOfTouchesRequired =1;
sender.direction = UISwipeGestureRecognizerDirectionLeft;
NSLog(@"向左轻扫手势");
}
// 动画开关按钮的回调方法
-(void)buttonAction:(UIButton*)sender{
UIImageView imageView = (UIImageView)[self.window viewWithTag:3000];
if (imageView.isAnimating) {
[imageView stopAnimating];
[imageView setImage:[UIImage imageNamed:@"11.png"]];
}else{
imageView.image = nil;
[imageView startAnimating];
}
}