首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

sdk3.2手势范例

2012-07-15 
sdk3.2手势实例#import UIKit/UIKit.h@interface GesturesViewController : UIViewController {IBOutlet

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];
}

热点排行