MainViewController.m
#import "MainViewController.h"
@interface MainViewController ()
@property(nonatomic, retain)UIImageView *imageView;
@property(nonatomic, retain)UIAlertView *alertView;
@end
@implementation MainViewController
-(void)dealloc
{
[_imageView release];
[_alertView release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
// UIImageView
UIImage *image = [UIImage imageNamed:@"dsa.png"];
self.imageView = [[UIImageView alloc] initWithImage:image];
self.imageView.frame = CGRectMake(32, 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;
// yunxu用户手指长按过程中允许移动距离
longPress.allowableMovement = 200;
// 把手势添加到图片上
[self.imageView addGestureRecognizer:longPress];
[longPress release];
// 3.旋转
// 创建一个旋转地手势
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
// 把手势放在对应的图片上
[self.imageView addGestureRecognizer:rotation];
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(rotatingAction:) userInfo:nil repeats:YES];
// 释放
[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];
// 轻扫的方向
// 向右
// 7.0 屏幕边际手势, iOS7.0之后出现的手势
// UIScreenEdgePanGestureRecognizer
UIStepper *stepper = [[UIStepper alloc] initWithFrame:CGRectMake(100, 500, 50, 30)];
[self.view addSubview:stepper];
[stepper addTarget:self action:@selector(updateValue:) forControlEvents:UIControlEventValueChanged];
stepper.minimumValue = 0;
stepper.maximumValue = 100;
// 循环
stepper.wraps = YES;
// 控制是否持续触发UIControlEventValueChanged事件。默认为YES,即当按住时每次值改变都触发一次UIControlEventValueChanged事件,否则只有在释放时触发UIControlEventValueChanged事件。
stepper.continuous = YES;
// 初始值
stepper.value = 20;
// 每次增加或减少的量
stepper.stepValue = 5;
// 按住后是否增加或减少
stepper.autorepeat = YES;
[stepper release];
}
-(void)rotatingAction:(UIRotationGestureRecognizer *)rotation
{
UIImageView *imageView = (UIImageView *)rotation.view;
// 进行旋转操作
// 通过视图的transform属性,让视图进行旋转
imageView.transform = CGAffineTransformRotate(imageView.transform, rotation.rotation);
}
-(void)updateValue:(UIStepper *)stepper
{
NSLog(@"2");
NSLog(@"%g", stepper.value);
}
-(void)swipeAction:(UISwipeGestureRecognizer *)swipe
{
if (swipe.direction == UISwipeGestureRecognizerDirectionRight ) {
NSLog(@"向右");
}
}
-(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)pinchAction:(UIPinchGestureRecognizer *)pinch
{
// 通过手势找视图
// 第一个参数 图片的原始尺寸, 第二,第三个参数 手势的缩放比例
UIImageView *imageView = (UIImageView *)pinch.view;
imageView.transform = CGAffineTransformScale(imageView.transform, pinch.scale, pinch.scale);
// 为了防止手势的变化使图片直接消失,起终止作用
pinch.scale = 1;
}
#pragma mark 通过图片的旋转手势,让图片发生旋转
-(void)rotationAction:(UIRotationGestureRecognizer *)rotation
{
// 可以通过手势获取手势添加的视图是哪一个
UIImageView *imageView = (UIImageView *)rotation.view;
// 进行旋转操作
// 通过视图的transform属性,让视图进行旋转
imageView.transform = CGAffineTransformRotate(imageView.transform, rotation.rotation);
NSLog(@"%g", rotation.rotation);
rotation.rotation = 0;
}
#pragma mark 长按方法
-(void)longPressAction:(UILongPressGestureRecognizer *)longPress
{
// 长按的状态
// longPress.state
// 长按之后弹出一个UIAlertView
if (!self.alertView) {
self.alertView = [[UIAlertView alloc] initWithTitle:@"1" message:@"2" delegate:self cancelButtonTitle:@"1" otherButtonTitles:@"2", nil];
}
[self.alertView show];
}
#pragma mark 点击方法
-(void)tapAction:(UITapGestureRecognizer *)tap
{
NSLog(@"hgfdh");
self.imageView.image = [UIImage imageNamed:@"thumb.png"];
}