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

UI05_手势识别器

2015-08-06 08:51 543 查看
//
//  MainViewController.m
//  UI05_手势识别器
//
//  Created by dllo on 15/8/4.
//  Copyright (c) 2015年 Clare. All rights reserved.
//

#import "MainViewController.h"

@interface MainViewController ()

@property(nonatomic, retain)UIImageView *imageView;
@property(nonatomic, assign)BOOL changePic;
@property(nonatomic, retain)UIAlertView *alertView;
@property(nonatomic, retain)UIStepper *stepper;
@end

@implementation MainViewController

- (void)dealloc
{
[_imageView release];
[_alertView release];
[_stepper release];
[super dealloc];
}

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor lightGrayColor];

// UIImageView
UIImage *image = [UIImage imageNamed:@"i.jpg"];
self.imageView = [[UIImageView alloc] initWithImage:image];
self.imageView.frame = CGRectMake(40, 100, 300, 500);
[self.view addSubview:self.imageView];
[_imageView release];

/// 把图片的用户交互打开,它默认是关闭的,此外还有一个控件是label
self.imageView.userInteractionEnabled = YES;

//// 手势的使用
/// 1.点击
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
// 设置点击几次才会触发方法
tap.numberOfTapsRequired = 2;
// 设置几根手指进行点击
tap.numberOfTouchesRequired = 2;
// 将手势添加到对应的图片上
[self.imageView addGestureRecognizer:tap];
[tap release];

/// 2.长按
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
// 设置长按触发的最小时间
longPress.minimumPressDuration = 2;
// 用户手指在长按的过程中允许移动距离
longPress.allowableMovement = 200;
// 把手势添加到对应的图片上
[self.imageView addGestureRecognizer:longPress];
[longPress release];

//    self.alertView = [[UIAlertView alloc] initWithTitle:@"切换图片" message:@"图案" delegate:self cancelButtonTitle:@"返回" otherButtonTitles:nil, nil];
//    [self.alertView release];

/// 3.旋转
// 创建一个旋转手势
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
// 把手势放到对应的图片上
[self.imageView addGestureRecognizer:rotation];
// 释放
[rotation release];

/// 4.捏合
// 创建
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
// 添加到图片上
[self.imageView addGestureRecognizer:pinch];
//释放
[pinch release];

/// 5.拖拽
//    // 创建手势
//    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
//    // 添加手势
//    [self.imageView addGestureRecognizer:pan];
//    // 释放
//    [pan release];

/// 6.轻扫
// 创建手势
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
// 添加手势
[self.imageView addGestureRecognizer:swipe];
// 释放
[swipe release];

// 轻扫的方向
// 向右
swipe.direction = UISwipeGestureRecognizerDirectionRight;

/// 7.屏幕边际手势,iOS7.0之后出现的手势
//    UIScreenEdgePanGestureRecognizer

self.stepper = [[UIStepper alloc] init];
self.stepper.minimumValue = image.size.width / 2;
self.stepper.maximumValue = image.size.height > image.size.width ? image.size.height :image.size.width;
self.stepper.stepValue = 20;
[self.imageView addSubview:self.stepper];
[self.stepper release];
[self.stepper addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];

}

#pragma mark 点击的方法
- (void)tapAction:(UITapGestureRecognizer *)tap
{
NSLog(@"测试手势");
if (self.changePic) {
self.imageView.image = [UIImage imageNamed:@"i.jpg"];
} else {
self.imageView.image = [UIImage imageNamed:@"j.jpg"];
}
self.changePic = !self.changePic;
}

#pragma mark 长按对应的响应方法
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress
{
// 长按的状态
//longPress.state;

// alertView 在didload中创建的方法
//[self.alertView show];

// 长按之后弹出一个UIAlertView
// alertView在方法中直接创建的方法
if (!self.alertView) {
self.alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"是否切换图片" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:@"返回", nil];
[self.alertView show];
[_alertView release];
self.alertView = nil;
}
}

#pragma mark 通过图片的旋转手势,让图片发生旋转
- (void)rotationAction:(UIRotationGestureRecognizer *)rotation
{
// 可以通过手势获取手势添加的视图是哪一个
UIImageView *imageView = (UIImageView *)rotation.view;
// 进行旋转的操作
// 通过视图的transform属性,让视图进行旋转
imageView.transform = CGAffineTransformRotate(imageView.transform, rotation.rotation);
rotation.rotation = 0;
}

#pragma mark 通过捏合手势,缩放图片
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch
{
// 通过手势找视图
UIImageView *imageView = (UIImageView *)pinch.view;

// 通过transform改变图片的尺寸
imageView.transform = CGAffineTransformScale(imageView.transform, pinch.scale, pinch.scale);
// 为了防止手势的变化让图片直接消失
pinch.scale = 1;
}

#pragma mark 通过拖拽手势,让视图随手势的移动而移动
- (void)panAction:(UIPanGestureRecognizer *)pan
{
// 通过手势找视图
UIImageView *imageView = (UIImageView *)pan.view;
// 通过手势获得经过的点
CGPoint p =[pan translationInView:imageView];
// 设置移动的位置
imageView.transform =CGAffineTransformTranslate(imageView.transform, p.x, p.y);
// 为了防止手势在操作的时候视图突然消失
[pan setTranslation:CGPointZero inView:imageView];
}

#pragma mark 轻扫的对应的方法
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe
{
// 通过手势找视图
if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
NSLog(@"向右");
}

}

- (IBAction)valueChange:(UIStepper *)sender
{
int stepValue = sender.value;
self.imageView.bounds = CGRectMake(self.imageView.bounds.origin.x, self.imageView.bounds.origin.y, stepValue, stepValue);

}

- (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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: