最近用到了手势,把手势的应用做了个小综合,拿出来和大家分享一下
ViewController.h
//
// ViewController.h
// SignView
//
// Created by Eleven on 13-5-4.
// Copyright (c) 2013年 mac. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
UIView *myView;
UIImageView *myImageView;
}
@end
ViewController.m
//
// ViewController.m
// SignView
//
// Created by Eleven on 13-5-4.
// Copyright (c) 2013年 mac. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (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.
myView=[[UIView alloc] initWithFrame:CGRectMake(50, 100, 200, 200)];
myView.backgroundColor=[UIColor redColor];
[self.view addSubview:myView];
[myView release];
//设置gif动作的图片
myImageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
NSMutableArray *array=[[NSMutableArray alloc] init];
for (int i=1; i<=7; i++) {
NSString *imageName=[NSString stringWithFormat:@"huoju_%d.tiff",i];
UIImage *image=[UIImage imageNamed:imageName];
[array addObject:image];
}
myImageView.animationImages=array;
myImageView.animationDuration=1;
[myView addSubview:myImageView];
[myImageView release];
UISlider *slider=[[UISlider alloc] initWithFrame:CGRectMake(50, 50, 200, 20)];
[self.view addSubview:slider];
[slider addTarget:self action:@selector(changeSlider:) forControlEvents:UIControlEventValueChanged];
[slider release];
UIButton *startButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
startButton.frame=CGRectMake(50, 350, 50, 50);
[startButton addTarget:self action:@selector(startAction:) forControlEvents:UIControlEventTouchUpInside];
[startButton setTitle:@"start" forState:UIControlStateNormal];
[self.view addSubview:startButton];
UIButton *stopButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
stopButton.frame=CGRectMake(220, 350, 50, 50);
[stopButton addTarget:self action:@selector(stopAction:) forControlEvents:UIControlEventTouchUpInside];
[stopButton setTitle:@"stop" forState:UIControlStateNormal];
[self.view addSubview:stopButton];
NSArray *arrays=[NSArray arrayWithObjects:@"轻拍",@"长按",@"轻扫",@"旋转",@"捏合",@"拖拽", nil];
UISegmentedControl *segment=[[UISegmentedControl alloc] initWithItems:arrays];
segment.frame=CGRectMake(0, 430, 320, 30);
[segment addTarget:self action:@selector(changeSign:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segment];
[segment release];
}
-(void)startAction:(UIImageView *)image
{
NSLog(@"start");
[myImageView startAnimating];
}
-(void)stopAction:(UIImageView *)image
{
NSLog(@"stop");
[myImageView stopAnimating];
}
-(void)changeSlider:(UISlider *)slider
{
if (myImageView.isAnimating) {
[myImageView stopAnimating];
}
myImageView.animationDuration=1-slider.value;
[myImageView startAnimating];
}
-(void)changeSign:(UISegmentedControl *)sign
{
NSArray *arrayAction=[NSArray array];
arrayAction=[myView gestureRecognizers];
for (int i=0; i<[arrayAction count]; i++) {
NSLog(@"%@",arrayAction);
[myView removeGestureRecognizer:arrayAction[i]];
}
switch (sign.selectedSegmentIndex) {
case 0:
NSLog(@"轻拍");
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
tap.numberOfTapsRequired=1;
[myView addGestureRecognizer:tap];
[tap release];
break;
case 1:
NSLog(@"长按");
UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
longPress.minimumPressDuration=3;//长按3秒后触发事件
[myView addGestureRecognizer:longPress];
[longPress release];
break;
case 2:
NSLog(@"轻扫");
//向左轻扫
UISwipeGestureRecognizer* swipeRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction1:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[myView addGestureRecognizer:swipeRight];
[swipeRight release];
//向右轻扫
UISwipeGestureRecognizer* swipeLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction2:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[myView addGestureRecognizer:swipeLeft];
[swipeLeft release];
//向上轻扫
UISwipeGestureRecognizer* swipeUp = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction3:)];
swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
[myView addGestureRecognizer:swipeUp];
[swipeUp release];
//向下轻扫
UISwipeGestureRecognizer *swipeDown =[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction4:)];
swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
[myView addGestureRecognizer:swipeDown];
[swipeDown release];
break;
case 3:
NSLog(@"旋转");
UIRotationGestureRecognizer *rot=[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotAction:)];
[myView addGestureRecognizer:rot];
[rot release];
break;
case 4:
NSLog(@"捏合");
UIPinchGestureRecognizer *pin=[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinAction:)];
[myView addGestureRecognizer:pin];
[pin release];
break;
case 5:
NSLog(@"拖拽");
UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
[myView addGestureRecognizer:pan];
[pan release];
break;
default:
break;
}
}
//轻扫
- (void)swipeAction1:(UISwipeGestureRecognizer *)recognizer
{
CGPoint point = [recognizer locationInView:[self view]];
NSLog(@"Swipe right: %f,%f", point.x, point.y);
}
- (void)swipeAction2:(UISwipeGestureRecognizer *)recognizer
{
CGPoint point = [recognizer locationInView:[self view]];
NSLog(@"Swipe left: %f,%f", point.x, point.y);
}
- (void)swipeAction3:(UISwipeGestureRecognizer *)recognizer
{
CGPoint point = [recognizer locationInView:[self view]];
NSLog(@"Swipe up: %f,%f", point.x, point.y);
}
- (void)swipeAction4:(UISwipeGestureRecognizer *)recognizer
{
CGPoint point = [recognizer locationInView:[self view]];
NSLog(@"Swipe down: %f,%f", point.x, point.y);
}
//轻点
-(void)tapAction
{
NSLog(@"tapAction");
self.view.backgroundColor=[UIColor yellowColor];
}
//长按
-(void)longPressAction:(UILongPressGestureRecognizer *)longPress
{
NSLog(@"longPressAction");
self.view.backgroundColor=[UIColor greenColor];
}
//捏合
-(void)pinAction:(UIPinchGestureRecognizer *)gestre
{
NSLog(@"pinAction");
UIView *myNewView=gestre.view;
myNewView.transform=CGAffineTransformMakeScale(gestre.scale, gestre.scale);
}
//旋转
-(void)rotAction:(UIRotationGestureRecognizer *)gesture
{
NSLog(@"rotAction");
UIView *myNewView=gesture.view;
myNewView.transform=CGAffineTransformMakeRotation(gesture.rotation);
}
//拖拽
-(void)panAction:(UIPanGestureRecognizer *)gesture
{
NSLog(@"panAction");
UIView *myNewView=gesture.view;
CGPoint point=[gesture translationInView:self.view];
//动画偏移
myNewView.transform=CGAffineTransformMakeTranslation(point.x, point.y);
// NSLog(@"%@",NSStringFromCGPoint(point));
//
// //坐标偏移
// CGPoint center=myView.center;
// center.x += point.x;
// center.y += point.y;
// myView.center=center;
// //- (void)setTranslation:(CGPoint)translation inView:(UIView *)view;
// [gesture setTranslation:CGPointZero inView:self.view];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
注:我刚才发现了很诡异的问题,在触摸板上直接操作,没有用鼠标,里面的旋转和捏合手势不能响应,找了半天问题也没发现错误在哪儿,后来我又把之前的程序运行,也是不能响应了,我还以为电脑突然出了问题.想了想,把鼠标插上重新运行,没问题了,不再查看相关的资料了,记住使用手势的时候用鼠标操作吧,休息了,呵呵^.^