<AppDelegate.m>
#import "AppDelegate.h" #import "RootViewController.h" @implementation AppDelegate - (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]; RootViewController *rootVC = [[RootViewController alloc ]init ]; self.window.rootViewController = rootVC; [rootVC release]; return YES; } - (void)applicationWillResignActive:(UIApplication *)application { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - (void)applicationWillEnterForeground:(UIApplication *)application { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } - (void)applicationDidBecomeActive:(UIApplication *)application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } @end
<RootViewController.h>
#import <UIKit/UIKit.h> @interface RootViewController : UIViewController @end
<RootViewController.m>
#import "RootViewController.h" @interface RootViewController () { BOOL _isFirst; } @property (nonatomic,retain)UIImageView *imageView;//图片视图 @end @implementation RootViewController -(void)dealloc { self.imageView = nil; [super dealloc]; } - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //布局imageView [self layoutImageView]; //创建手势 [self creatRecognizers]; } //布局imageView -(void)layoutImageView { //创建对象 self.imageView = [[UIImageView alloc] initWithFrame:self.view.bounds]; //2.配置属性 //设置 //1) _imageView.image = [UIImage imageNamed:@"01.png"]; //2) _imageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"01" ofType:@"png"]];//这个表示资源的名字和资源的种类,也可以写在一起,后面写 nil ,pathForResource:@"01.png" ofType:nil . _imageView.image = [UIImage imageWithContentsOfFile:@"<span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">/Users/MyMac/Desktop/文件/学习/UI/5-3.LessonUIImageView/LessonUIImageView/01.png</span><span style="font-family: Arial, Helvetica, sans-serif;">"];</span> //以上三种方法,第1个,第2个需要把图片先拖进来,第3个不用. 而第2个的特点是执行此代码前,它就已经进包了,而其他的都是等整个运行时进包的,,推荐使用第2个方法 //设置图片等比例展示 //计算图片的比例 CGFloat rate = _imageView.image.size.height / _imageView.image.size.width; //重新设置 frame // _imageView.frame = CGRectMake(0, 0, 320, 320 * rate); //在父视图左上角 _imageView.bounds = CGRectMake(0, 0, 320, 320 * rate); //3.添加到父视图 [self.view addSubview:_imageView]; //4.释放所有权 [_imageView release]; } //创建手势 -(void)creatRecognizers { //七大手势 //1.轻拍手势 //1)创建对象 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc ] initWithTarget:self action:@selector(tapAction:)]; //2)配置属性 //设置手指个数 tap.numberOfTouchesRequired = 1; //设置轻拍次数 tap.numberOfTapsRequired = 1; //2.添加到指定视图上 [_imageView addGestureRecognizer:tap]; //打开用户交互 _imageView.userInteractionEnabled = YES; _isFirst = YES; //3.释放 [tap release]; //移除手势 [_imageView removeGestureRecognizer:tap]; //2.轻扫手势 //创建 UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)]; //配置属性 //设置轻扫方向 一个轻扫手势只能设置一对方向,如果想设置四个方向的轻扫结果,就需要创建两个轻扫手势 swipe.direction = UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown; //添加到指定视图 [_imageView addGestureRecognizer:swipe]; //释放 [swipe release]; //3.长按手势 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc ] initWithTarget:self action:@selector(longPressAction:)]; //设置长按时间,时间达到1秒才视为长按 longPress.minimumPressDuration = 1; //设置长按时允许移动的距离,超出此距离就不视为长按 longPress.allowableMovement = 10; [_imageView addGestureRecognizer:longPress]; [longPress release]; //4.平移手势 UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)]; [_imageView addGestureRecognizer:pan]; [_imageView removeGestureRecognizer:pan]; [pan release]; //5.捏合手势 UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)]; [_imageView addGestureRecognizer:pinch]; [pinch release]; //6.旋转手势 UIRotationGestureRecognizer *rota = [[UIRotationGestureRecognizer alloc ]initWithTarget:self action:@selector(rotaAction:)]; [_imageView addGestureRecognizer:rota]; [rota release]; //7.屏幕边缘手势 UIScreenEdgePanGestureRecognizer *screenEdge = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgeAction:)]; //设置边缘的位置 screenEdge.edges = UIRectEdgeLeft; [_imageView addGestureRecognizer:screenEdge]; [screenEdge release]; } //1.轻拍手势 -(void)tapAction:(UITapGestureRecognizer *)tap { NSLog(@"拍了一下"); if (_isFirst) { _imageView.image = [UIImage imageNamed:@"02.png"]; _isFirst = NO; }else{ _imageView.image = [UIImage imageNamed:@"01.png"]; _isFirst = YES; } } //2.轻扫手势 -(void)swipeAction:(UISwipeGestureRecognizer *)swipe { //根据轻扫方向更换图片 if (_isFirst) { _imageView.image = [UIImage imageNamed:@"02.png"]; _isFirst = NO; }else{ _imageView.image = [UIImage imageNamed:@"01.png"]; _isFirst = YES; } } //3.长按手势 -(void)longPressAction:(UILongPressGestureRecognizer *)longPress { //提示是否保存图片 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您是否要保存图片?" delegate: self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil]; //longpress 判断手势状态 //当长按手势结束时 显示 alertView if (longPress.state == UIGestureRecognizerStateBegan ){ [alertView show]; [alertView release]; } //将图片保存到本地相册的方法 ============================ // UIImageWriteToSavedPhotosAlbum(UIImage *image, id completionTarget, SEL completionSelector, void *contextInfo); // UIImageWriteToSavedPhotosAlbum(_imageView, self, @selector(image: didFinishSavingWithError: contextInfo: ), nil); } //4.平移手势 -(void)panAction:(UIPanGestureRecognizer *)pan { //获取位置 CGPoint position = [pan translationInView:_imageView]; //根据transform进行视图平移 _imageView.transform = CGAffineTransformTranslate(_imageView.transform, position.x, position.y); //将增量值为零 [pan setTranslation:CGPointZero inView:_imageView]; } //5.捏合手势 -(void)pinchAction:(UIPinchGestureRecognizer *)pinch { //通过 transform 进行视图捏合 _imageView.transform = CGAffineTransformScale(_imageView.transform, pinch.scale, pinch.scale); pinch.scale = 1; } //6.旋转手势 -(void)rotaAction:(UIRotationGestureRecognizer *)rota { //通过 transform 进行视图旋转 _imageView.transform = CGAffineTransformRotate(_imageView.transform, rota.rotation); //旋转角度置为0 rota.rotation = 0; } //7.屏幕边缘手势 -(void)screenEdgeAction:(UIScreenEdgePanGestureRecognizer *)screenEdge { NSLog(@"aaaa"); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end