import "AppDelegate.h"
@interface AppDelegate ()<UIGestureRecognizerDelegate>
@property (nonatomic ,retain)UIImageView *imageView;
@end
@implementation AppDelegate
// 自定義按鈕的回調(diào)
-(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];// 初始化一個視圖(響應(yīng)者)來承載手勢
UIView *gestureView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
// 當(dāng)前視圖放置在屏幕中
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];// 初始化UIImageViewUIImageView *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;
// 設(shè)置動畫執(zhí)行的時長(單位為秒)
imageView.animationDuration = 1.5;
// 開始動畫
[imageView startAnimating];
// 動畫結(jié)束
[imageView stopAnimating];imageView.image = [UIImage imageNamed:@"11.png"];
// UIImageView 的用戶交互默認(rèn)是關(guān)閉的,要想使他處理觸摸事件,我們需要手動打開它
imageView.userInteractionEnabled = YES;
[self.window addSubview:imageView];// 創(chuàng)建開啟、結(jié)束動畫的按鈕
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:)];// 設(shè)置觸控對象(觸控點(diǎn)個數(shù))
[tapGR setNumberOfTouchesRequired:1];
// 設(shè)置輕拍的次數(shù)(點(diǎn)擊次數(shù))
[tapGR setNumberOfTapsRequired:2];
// 給創(chuàng)建好的視圖添加手勢(一個視圖可以添加多個手勢,但是一個手勢只能添加到一個視圖上面)
// [gestureView addGestureRecognizer:tapGR];
// 點(diǎn)擊屏幕空白處回收鍵盤(取消textField的第一響應(yīng)者)
// [self.window addGestureRecognizer:tapGR]; // 回收鍵盤所用
// 給圖片添加手勢
[imageView addGestureRecognizer:tapGR];// 捏合手勢
UIPinchGestureRecognizer *pinchGR = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
[imageView addGestureRecognizer:pinchGR];
pinchGR.delegate = self;// 旋轉(zhuǎn)手勢
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 -- 手勢的代理方法
// 使得多個手勢可以同時相應(yīng)
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
// 返回值為YES時,當(dāng)執(zhí)行一個手勢的操作的時候,也可以執(zhí)行其他手勢的操作
return YES;
}
// 輕拍手勢的回調(diào)方法
-(void)tapAction:(UITapGestureRecognizer*)sender{
// 可以根據(jù)手勢得到它當(dāng)前所作用的視圖
// UIImageView imageView = (UIImageView)sender.view;
// imageView.frame = CGRectMake(50, 50, 50, 50);
NSLog(@"我輕拍了gestureView");
// // 得到textField(注意類型強(qiáng)轉(zhuǎn))
// UITextField tagTextField = (UITextField)[self.window viewWithTag:1000];
// // 回收鍵盤 (取消第一響應(yīng)者)
// [tagTextField resignFirstResponder];
}
// 捏合手勢的回調(diào)方法
-(void)pinchAction:(UIPinchGestureRecognizer*)sender{
// 通過捏合手勢得到縮放比例
float scale = sender.scale;
NSLog(@"%.1f",scale);
// 得到該手勢作用的視圖
UIView *view = sender.view;
// 2D仿射變換函數(shù)中 的縮放函數(shù)來實(shí)現(xiàn)視圖的放大縮小
// 是在原有基礎(chǔ)上改變當(dāng)前視圖
// <#CGAffineTransform t#>參數(shù):現(xiàn)有視圖的transform值
// CGFloat sx 參數(shù):x軸上的縮放比例
// <#CGFloat sy#>參數(shù):y軸上的縮放比例
// view.transform = CGAffineTransformMakeScale(0.5, 0.5); // 在視圖最初(初始狀態(tài))的transform狀態(tài)上改變,不管執(zhí)行多少次,都是以最初的transform狀態(tài)為基礎(chǔ)來改變的
view.transform = CGAffineTransformScale(view.transform, scale, scale);
// 每次捏合動作完畢之后,上次捏合值復(fù)原,使它每次都是從100%開始縮放
sender.scale = 1;
NSLog(@"捏合手勢");
}
// 旋轉(zhuǎn)手勢的回調(diào)方法
-(void)rotationAction:(UIRotationGestureRecognizer*)sender{
// 通過該手勢得到旋轉(zhuǎn)角度
float rotation = sender.rotation;
// 得到該手勢作用的視圖
UIView *view = sender.view;
// 通過2D仿射變換函數(shù)中的旋轉(zhuǎn)函數(shù)來使得當(dāng)前視圖旋轉(zhuǎn)
// <#CGAffineTransform t#>參數(shù):現(xiàn)有視圖的transform值(矩陣)
// <#CGFloat angle#>參數(shù):旋轉(zhuǎn)角度
view.transform = CGAffineTransformRotate(view.transform, rotation);
// 旋轉(zhuǎn)之后復(fù)原
sender.rotation = 0;
}
// 平移手勢的回調(diào)方法
-(void)panAction:(UIPanGestureRecognizer*)sender{
// 得到該手勢作用的視圖
UIView *view = sender.view;
// 得到我們在視圖上移動的偏移量
CGPoint currentPoint = [sender translationInView:view.superview];
// 通過2D仿射變換函數(shù)中與位移有關(guān)的函數(shù)實(shí)現(xiàn)視圖位置變化
view.transform = CGAffineTransformTranslate(view.transform, currentPoint.x , currentPoint.y);
// 復(fù)原
[sender setTranslation:CGPointZero inView:view.superview];
NSLog(@"平移手勢");
}
// 屏幕邊緣輕掃手勢回調(diào)方法
-(void)screenEdgePanAction:(UIScreenEdgePanGestureRecognizer*)sender{
NSLog(@"屏幕邊緣輕掃手勢");
}
// 長按手勢的回調(diào)方法
-(void)longPressAction:(UILongPressGestureRecognizer*)sender{
// 設(shè)置當(dāng)前長按最小的時長
sender.minimumPressDuration = 1.5;
// 設(shè)置允許移動的范圍
sender.allowableMovement = 2;
NSLog(@"長按手勢");
}
// 輕掃手勢
-(void)swipeAction:(UISwipeGestureRecognizer*)sender{
sender.numberOfTouchesRequired =1;
sender.direction = UISwipeGestureRecognizerDirectionLeft;
NSLog(@"向左輕掃手勢");
}
// 動畫開關(guān)按鈕的回調(diào)方法
-(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];
}
}