sdk3.2手势实例

#import <UIKit/UIKit.h>
@interface GesturesViewController : UIViewController {
IBOutlet UIImageView *imageView;
}
@property (nonatomic, retain) UIImageView *imageView;
@end

 

 

 

#import “GesturesViewController.h”
@implementation GesturesViewController
@synthesize imageView;

 

CGFloat lastScaleFactor = 1;

CGFloat netRotation;

CGPoint netTranslation;

NSArray *images;
int imageIndex = 0;

 


- (void)viewDidLoad {

 


//---tap gesture---
UITapGestureRecognizer *tapGesture =[[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 2;
[imageView addGestureRecognizer:tapGesture];
[tapGesture release];

 

//---pinch gesture---
UIPinchGestureRecognizer *pinchGesture =[[UIPinchGestureRecognizer alloc]
initWithTarget:self
action:@selector(handlePinchGesture:)];
[imageView addGestureRecognizer:pinchGesture];
[pinchGesture release];

 

//---rotate gesture---
UIRotationGestureRecognizer *rotateGesture =
[[UIRotationGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleRotateGesture:)];
[imageView addGestureRecognizer:rotateGesture];
[rotateGesture release];

 

//---pan gesture---
UIPanGestureRecognizer *panGesture =
[[UIPanGestureRecognizer alloc]
initWithTarget:self
action:@selector(handlePanGesture:)];
[imageView addGestureRecognizer:panGesture];
[panGesture release];

 

//---swipe gesture---
images = [[NSArray alloc] initWithObjects:@“architecture.jpg”,
@“Buildings.jpeg”,
@“Bridge.jpeg”, nil];
//---right swipe (default)---
UISwipeGestureRecognizer *swipeGesture =
[[UISwipeGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleSwipeGesture:)];
[imageView addGestureRecognizer:swipeGesture];
[swipeGesture release];
//---left swipe---
UISwipeGestureRecognizer *swipeLeftGesture =
[[UISwipeGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleSwipeGesture:)];
swipeLeftGesture.direction = UISwipeGestureRecognizerDirectionLeft;

 

[imageView addGestureRecognizer:swipeLeftGesture];
[swipeLeftGesture release];

 

 //---long press gesture---
UILongPressGestureRecognizer *longpressGesture =
[[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleLongpressGesture:)];
longpressGesture.minimumPressDuration = 1;
longpressGesture.allowableMovement = 15;
longpressGesture.numberOfTouchesRequired = 1;
[imageView addGestureRecognizer:longpressGesture];
[longpressGesture release];


[super viewDidLoad];
}

 


//---handle tap gesture---
-(IBAction) handleTapGesture:(UIGestureRecognizer *) sender {
if (sender.view.contentMode == UIViewContentModeScaleAspectFit)
sender.view.contentMode = UIViewContentModeCenter;
else
sender.view.contentMode = UIViewContentModeScaleAspectFit;
}

 

//---handle pinch gesture---
-(IBAction) handlePinchGesture:(UIGestureRecognizer *) sender {
CGFloat factor = [(UIPinchGestureRecognizer *) sender scale];
if (factor > 1) {
//---zooming in---
sender.view.transform = CGAffineTransformMakeScale(
lastScaleFactor + (factor-1),
lastScaleFactor + (factor-1));
} else {
//---zooming out---
sender.view.transform = CGAffineTransformMakeScale(
lastScaleFactor * factor,
lastScaleFactor * factor);
}
if (sender.state == UIGestureRecognizerStateEnded){
if (factor > 1) {
lastScaleFactor += (factor-1);
} else {
lastScaleFactor *= factor;
}
}
}

 

 

//---handle rotate gesture---
-(IBAction) handleRotateGesture:(UIGestureRecognizer *) sender {
CGFloat rotation = [(UIRotationGestureRecognizer *) sender rotation];
CGAffineTransform transform = CGAffineTransformMakeRotation(
rotation + netRotation);
sender.view.transform = transform;

if (sender.state == UIGestureRecognizerStateEnded){
netRotation += rotation;
}
}

 

 

//---handle pan gesture---
-(IBAction) handlePanGesture:(UIGestureRecognizer *) sender {
CGPoint translation = [(UIPanGestureRecognizer *) sender translationInView:imageView];
sender.view.transform = CGAffineTransformMakeTranslation(
netTranslation.x + translation.x,
netTranslation.y + translation.y);
if (sender.state == UIGestureRecognizerStateEnded){
netTranslation.x += translation.x;
netTranslation.y += translation.y;
}
}

 

 

//---handle swipe gesture---
-(IBAction) handleSwipeGesture:(UIGestureRecognizer *) sender {
UISwipeGestureRecognizerDirection direction =
[(UISwipeGestureRecognizer *) sender direction];

switch (direction) {
case UISwipeGestureRecognizerDirectionUp:
NSLog(@“up”);
break;
case UISwipeGestureRecognizerDirectionDown:
NSLog(@“down”);
break;
case UISwipeGestureRecognizerDirectionLeft:
imageIndex++;
break;
case UISwipeGestureRecognizerDirectionRight:
imageIndex--;
break;
default:
break;
}
imageIndex = (imageIndex < 0) ? ([images count] - 1):
imageIndex % [images count];
imageView.image = [UIImage imageNamed:[images objectAtIndex:imageIndex]];
}

 

 //---handle long press gesture---
-(IBAction) handleLongpressGesture:(UIGestureRecognizer *) sender {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@“Image options”
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:@“Save Image”,
@“Copy”, nil];
//---remember to implement the UIActionSheetDelegate protocol in your
// view controller---
[actionSheet showInView:self.view];
[actionSheet release];
}

 


- (void)dealloc {
[imageView release];
[super dealloc];
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值