`

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

分享到:
评论

相关推荐

    Kinect v2 Examples with MS-SDK 2.10.1

    SDK 2.10.1”、描述“Kinect v2 Examples with MS-SDK 2.10.1”以及标签“unity kinect”,来深入探讨与Kinect v2传感器相关的技术细节及其在Unity开发环境中的应用实例。 ### 一、Kinect v2 介绍 #### 1.1 Kinect...

    Google_Android_SDK开发范例集锦

    通过创建一个节日相关的应用实例,展示如何结合文本、图像等多种元素,实现有趣的应用场景。 - **4.4 我同意条款** 介绍如何设计用户同意条款的界面,并确保用户能够明确同意相关条款。 - **4.5 消费券采购列表**...

    WINDOWS CE 6_0开发者参考(1-3章带书签).

    - **SDK集成**:讲解了如何将Windows CE 6.0 SDK集成到Visual Studio中,以便进行开发工作。 - **项目创建**:详细说明了如何在Visual Studio中创建一个新的Windows CE应用程序项目,并介绍了一些基本的项目设置。 ...

    opencv_tutorials2.4.3

    - **应用实例**:手指计数、手势识别等。 ##### 3.22 创建轮廓的边界框和圆 - **边界框**:使用 `boundingRect()` 函数计算轮廓的外接矩形。 - **边界圆**:使用 `minEnclosingCircle()` 函数计算轮廓的外接圆。 - ...

    Android入门到精通详解

    通过MotionEvent对象,可以获取触摸点的位置、压力等信息,这对于实现手势识别等功能非常有用。 **6.4 屏幕间的跳转和事件的传递** 在Android应用中,经常需要从一个Activity跳转到另一个Activity,以实现导航或...

Global site tag (gtag.js) - Google Analytics