您的位置:首页 > 产品设计 > UI/UE

iOS编程-------UIImageView、手势识别器UIGestureRecognizer

2015-10-04 23:04 387 查看
//
//  AppDelegate.h
//  UI05_UIImageView、UIGestureRecognizer
//
//  Created by l on 15/9/8.
//  Copyright (c) 2015年 . All rights reserved.
//

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

//
//  AppDelegate.m
//  UI05_UIImageView、UIGestureRecognizer
//
//  Created by l on 15/9/8.
//  Copyright (c) 2015年 . All rights reserved.
//

#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate
- (void)dealloc{
[_window release];
[super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
#pragma mark- 创建根视图
RootViewController *rootVC = [[RootViewController alloc] init];
self.window.rootViewController = rootVC;
[rootVC release];

return YES;
}

/////////////////////////////////////////////////////////////////////////////

//
//  RootViewController.h
//  UI05_UIImageView、UIGestureRecognizer
//
//  Created by l on 15/9/8.
//  Copyright (c) 2015年 . All rights reserved.
//

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController

@end

//
//  RootViewController.m
//  UI05_UIImageView、UIGestureRecognizer
//
//  Created by l on 15/9/8.
//  Copyright (c) 2015年 . All rights reserved.
//

#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad {
[super viewDidLoad];
//UIImageView
//1.开辟空间并且初始化
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:@"/Users/lanou/Desktop/iOS-课堂学习/UI05_UIImageView、UIGestureRecognizer/UI05_UIImageView、UIGestureRecognizer/sea.png"]];
//2.设置 属性frame
imageView.frame = CGRectMake(50, 50, 300, 100);
//UIImageView 的用户交互默认是关闭的,需要我们打开
imageView.userInteractionEnabled = YES;//打开用户交互
//3.添加
[self.view addSubview:imageView];
//4.释放
[imageView release];

// 给UIImageView  添加图片序列数组
// 1. 创建imageView
UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:(CGRectMake(80, 300, 200, 200))];

//2.设置
//(1)设置图片序列属性 animationImages
NSMutableArray *picArray = [NSMutableArray array];

for (int i = 1; i <= 22 ; i++) {
//图片名
NSString *imageName = [NSString stringWithFormat:@"Zombie%d.tiff", i];
//图片image
UIImage *image = [UIImage imageNamed:imageName];
//把图片对象添加到数组中
[picArray addObject:image];

}
//(2)给imageView 的动画图片序列赋值
imageView2.animationImages = picArray;
//(3)设置动画属性,持续时间,重复次数等
imageView2.animationDuration = 2;//2秒
imageView2.animationRepeatCount = 0;//设置为0,一直重复播放
//(4)开始动画
[imageView2 startAnimating];

//3.添加
[self.view addSubview:imageView2];
//4.释放
[imageView2 release];

//知识点三:手势识别器 UIGestureRecognizer
//(1)Tap
//1.创建手势识别器
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
//2.设置属性
tap.numberOfTapsRequired = 1;//单机,轻拍一次
tap.numberOfTouchesRequired = 1;//一个手指头
//3.把手势识别器添加到视图上
//    [imageView addGestureRecognizer:tap];
//4.释放
[tap release];

//(2)longPress
//1.创建
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
//2.设置
longPress.minimumPressDuration = 2;//需要长按时间2秒
//3.添加
//    [imageView addGestureRecognizer:longPress];
//4.释放
[longPress release];
//5.方法实现

//(3)pan平移手势
//1.创建
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
//2.设置
//3.添加
[imageView addGestureRecognizer:pan];
//4.释放
[pan release];
//5.方法实现,视图跟随手指移动

//(4)swipe 轻扫手势 离散手势 非持续性,没有changed状态
//1.创建
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
//2.设置
//注意:轻扫手势不需要设置轻扫方向,屏幕边缘轻扫需要设置轻扫方向
//3.添加
[imageView addGestureRecognizer:swipe];
//4.释放
[swipe  release];
//5.方法实现

//(5)screenEdgePan 屏幕边缘轻扫
UIScreenEdgePanGestureRecognizer *screenEdge = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgeAction:)];
//设置边缘轻扫属性(设置边缘轻扫方向)
screenEdge.edges = UIRectEdgeRight;//左侧才会检测到
//添加
[self.view addGestureRecognizer:screenEdge];
[screenEdge release];

//(6)rotation 旋转
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
[imageView addGestureRecognizer:rotation];
[rotation release];

//(7)pinch 捏合手势识别器
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
[imageView addGestureRecognizer:pinch];
[pinch release];

// Do any additional setup after loading the view.
}

#pragma mark- tap手势(轻拍)
- (void)tapAction:(UITapGestureRecognizer *)tap{
//tap 为传递过来的 手势识别器
NSLog(@"检测到了轻拍手势");
}
#pragma mark- longPress(长按手势)
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress{
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"检测到长按手势开始的时候执行");
}if (longPress.state == UIGestureRecognizerStateChanged) {
NSLog(@"触摸点改变的时候开始执行");
}if (longPress.state == UIGestureRecognizerStateEnded) {
NSLog(@"长按手势结束的时候执行");
}
//    NSLog(@"检测到了长按手势");
}
#pragma mark- pan(pan手势)
- (void)panAction:(UIPanGestureRecognizer *)pan{
//1.获取手势识别器所在的位置
UIView *imageView = pan.view;
//2.获取视图的变换点
CGPoint transPoint = [pan translationInView:imageView];
//3.改变视图的transform,对视图进行交换
//(1) 在每次进行的交换,都是基于视图的原始状态进行的变换 make开头的方法
//    imageView.transform = CGAffineTransformMakeTranslation(transPoint.x, transPoint.y);
//(2)基于上一次状态的进行的变换
imageView.transform = CGAffineTransformTranslate(imageView.transform, transPoint.x, transPoint.y);

//每次变化后,需要把变化归0
[pan setTranslation:(CGPointZero) inView:imageView];

}

#pragma mark-- 轻扫手势
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe{

if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
NSLog(@"向右轻扫");
}else if (swipe.direction == UISwipeGestureRecognizerDirectionLeft){

NSLog(@"向左轻扫");
}
}

#pragma mark- 屏幕边缘轻扫

- (void)screenEdgeAction:(UIScreenEdgePanGestureRecognizer *)screenEdge{
//添加判断
if (screenEdge.edges == UIRectEdgeRight) {
NSLog(@"从屏幕边缘左侧轻扫");
}
}

#pragma mark -- 旋转手势
- (void)rotationAction:(UIRotationGestureRecognizer *)rotationGesture{
//1.获取旋转视图
UIImageView *imageView = (UIImageView *)rotationGesture.view;
//2.旋转弧度
CGFloat radian = rotationGesture.rotation;
//3.对视图进行变换
//(1)基于原始状态变换
imageView.transform = CGAffineTransformMakeRotation(radian);

//    (2)基于上一次状态变换
rotationGesture.view.transform = CGAffineTransformRotate(rotationGesture.view.transform, rotationGesture.rotation);

//记住要归零
rotationGesture.rotation = 0;

}

#pragma mark-- 捏合手势

- (void)pinchAction:(UIPinchGestureRecognizer *)pinch{
//(1)基于原始状态进行缩放
pinch.view.transform = CGAffineTransformMakeScale(pinch.scale, pinch.scale);//宽高缩放比例相同,保持宽高1:1进行整体缩放
//(2)基于上一次状态进行缩放,记住缩放要归零
pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
//缩放归零
pinch.scale = 1.0;

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: