iOS 各種手勢

UITapGestureRecongnizer 檢測view上的單擊操作

新建Empty Application項目,在xib中添加ImageView控件,Mode爲Aspect Fit,選中User Interaction Enabled和Multiple Touch

HomeViewController.h

#import <UIKit/UIKit.h>

@interface HomeViewController : UIViewController
@property (retain, nonatomic) IBOutlet UIImageView *imageView;
@end


HomeViewController.m 



#import "HomeViewController.h"
@interface HomeViewController ()
@end
@implementation HomeViewController
@synthesize imageView;
- (void)viewDidLoad
{
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureHandler:)];
    tapGesture.numberOfTapsRequired = 2;
    [imageView addGestureRecognizer:tapGesture];
    [tapGesture release];

   [super viewDidLoad];
}
- (IBAction)tapGestureHandler:(UIGestureRecognizer *)sender{
    if (sender.view.contentMode == UIViewContentModeScaleAspectFit) {
        sender.view.contentMode = UIViewContentModeScaleAspectFill;
    } else {
        sender.view.contentMode = UIViewContentModeScaleAspectFit;
    }

}
- (void)dealloc {
    [imageView release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setImageView:nil];
    [super viewDidUnload];
}
@end

UIPinchGestureRecongnizer 檢測view上兩個手指的縮放操作

HomeViewController.m 
#import "HomeViewController.h"
@interface HomeViewController ()
@end
@implementation HomeViewController
@synthesize imageView;
CGFloat lastScaleValue = 1;
- (void)viewDidLoad
{

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureHandler:)];
    tapGesture.numberOfTapsRequired = 2;
    [imageView addGestureRecognizer:tapGesture];
    [tapGesture release];

    UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchGestureHandler:)];
    [imageView addGestureRecognizer:pinchGesture];
    [pinchGesture release];

    [super viewDidLoad];
}
- (IBAction)tapGestureHandler:(UIGestureRecognizer *)sender{
    if (sender.view.contentMode == UIViewContentModeScaleAspectFit) {
        sender.view.contentMode = UIViewContentModeScaleAspectFill;
    } else {
        sender.view.contentMode = UIViewContentModeScaleAspectFit;
    }

}
- (IBAction)pinchGestureHandler:(UIGestureRecognizer *)sender{
    CGFloat scaleValue = [(UIPinchGestureRecognizer *)sender scale];

//如果手指做放大操作,scaleValue的值大於等於1;如果手指是縮小操作,scaleValue的值小於1

    if (scaleValue > 1) {
        sender.view.transform = CGAffineTransformMakeScale(lastScaleValue + (scaleValue-1), lastScaleValue + (scaleValue-1));
    }else {
        sender.view.transform = CGAffineTransformMakeScale(lastScaleValue * scaleValue, lastScaleValue * scaleValue);
    } 

//用戶的手指離開了屏幕,將比例值記錄在 lastScaleValue 變量中

    if (sender.state == UIGestureRecognizerStateEnded) {
        if (scaleValue > 1) {
            lastScaleValue += (scaleValue - 1);
        }else {
            lastScaleValue += scaleValue;
        }
    }


}
- (void)dealloc {
    [imageView release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setImageView:nil];
    [super viewDidUnload];
}
@end

UIRotationGestureRecongnizer 檢測view的旋轉操作

#import "HomeViewController.h"
@interface HomeViewController ()
@end
@implementation HomeViewController
@synthesize imageView;
CGFloat lastScaleValue = 1;
CGFloat rotationValue;
- (void)viewDidLoad
{

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureHandler:)];
    tapGesture.numberOfTapsRequired = 2;
    [imageView addGestureRecognizer:tapGesture];
    [tapGesture release];

    UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchGestureHandler:)];
    [imageView addGestureRecognizer:pinchGesture];
    [pinchGesture release];

    UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGestureHandler:)];
    [imageView addGestureRecognizer:rotationGesture];
    [rotationGesture release];

    [super viewDidLoad];
}
- (IBAction)tapGestureHandler:(UIGestureRecognizer *)sender{
    if (sender.view.contentMode == UIViewContentModeScaleAspectFit) {
        sender.view.contentMode = UIViewContentModeScaleAspectFill;
    } else {
        sender.view.contentMode = UIViewContentModeScaleAspectFit;
    }

}
- (IBAction)pinchGestureHandler:(UIGestureRecognizer *)sender{
    CGFloat scaleValue = [(UIPinchGestureRecognizer *)sender scale];

    if (scaleValue > 1) {
        sender.view.transform = CGAffineTransformMakeScale(lastScaleValue + (scaleValue-1), lastScaleValue + (scaleValue-1));
    }else {
        sender.view.transform = CGAffineTransformMakeScale(lastScaleValue * scaleValue, lastScaleValue * scaleValue);
    } 

    if (sender.state == UIGestureRecognizerStateEnded) {
        if (scaleValue > 1) {
            lastScaleValue += (scaleValue - 1);
        }else {
            lastScaleValue += scaleValue;
        }
    }


}
- (IBAction)rotationGestureHandler:(UIGestureRecognizer *)sender{
    CGFloat rotation = [(UIRotationGestureRecognizer *)sender rotation];
    CGAffineTransform transform = CGAffineTransformMakeRotation(rotation + rotationValue);

    sender.view.transform = transform;

    if (sender.state == UIGestureRecognizerStateEnded) {
        rotationValue += rotation;
    }
}
- (void)dealloc {
    [imageView release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setImageView:nil];
    [super viewDidUnload];
}
@end

UIPanGestureRecongnizer 檢測view的拖拽操作

 #import "HomeViewController.h"
@interface HomeViewController ()
@end
@implementation HomeViewController
@synthesize imageView;
CGFloat lastScaleValue = 1;
CGFloat rotationValue;
CGPoint positionValue;
- (void)viewDidLoad
{

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureHandler:)];
    tapGesture.numberOfTapsRequired = 2;
    [imageView addGestureRecognizer:tapGesture];
    [tapGesture release];

    UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchGestureHandler:)];
    [imageView addGestureRecognizer:pinchGesture];
    [pinchGesture release];

    UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGestureHandler:)];
    [imageView addGestureRecognizer:rotationGesture];
    [rotationGesture release];

    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureHandler:)];
    [imageView addGestureRecognizer:panGesture];
    [panGesture release];

    [super viewDidLoad];
}
- (IBAction)tapGestureHandler:(UIGestureRecognizer *)sender{
    if (sender.view.contentMode == UIViewContentModeScaleAspectFit) {
        sender.view.contentMode = UIViewContentModeScaleAspectFill;
    } else {
        sender.view.contentMode = UIViewContentModeScaleAspectFit;
    }

}
- (IBAction)pinchGestureHandler:(UIGestureRecognizer *)sender{
    CGFloat scaleValue = [(UIPinchGestureRecognizer *)sender scale];

    if (scaleValue > 1) {
        sender.view.transform = CGAffineTransformMakeScale(lastScaleValue + (scaleValue-1), lastScaleValue + (scaleValue-1));
    }else {
        sender.view.transform = CGAffineTransformMakeScale(lastScaleValue * scaleValue, lastScaleValue * scaleValue);
    } 

    if (sender.state == UIGestureRecognizerStateEnded) {
        if (scaleValue > 1) {
            lastScaleValue += (scaleValue - 1);
        }else {
            lastScaleValue += scaleValue;
        }
    }


}
- (IBAction)rotationGestureHandler:(UIGestureRecognizer *)sender{
    CGFloat rotation = [(UIRotationGestureRecognizer *)sender rotation];
    CGAffineTransform transform = CGAffineTransformMakeRotation(rotation + rotationValue);

    sender.view.transform = transform;

    if (sender.state == UIGestureRecognizerStateEnded) {
        rotationValue += rotation;
    }
}
- (IBAction)panGestureHandler:(UIGestureRecognizer *)sender{
    CGPoint position = [(UIPanGestureRecognizer *)sender translationInView:imageView];
    sender.view.transform = CGAffineTransformMakeTranslation(position.x + position.x, 
                                                             position.y + position.y);

    if (sender.state == UIGestureRecognizerStateEnded) {
        positionValue.x += position.x;
        positionValue.y += position.y;
    }
}
- (void)dealloc {
    [imageView release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setImageView:nil];
    [super viewDidUnload];
}
@end

UISwipeGestureRecongnizer 檢測view的輕劃操作

#import "HomeViewController.h"
@interface HomeViewController ()
@end
@implementation HomeViewController
@synthesize imageView;
CGFloat lastScaleValue = 1;
CGFloat rotationValue;
CGPoint positionValue;
NSArray *images;
int imageIndex = 0;
- (void)viewDidLoad
{

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureHandler:)];
    tapGesture.numberOfTapsRequired = 2;
    [imageView addGestureRecognizer:tapGesture];
    [tapGesture release];

    UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchGestureHandler:)];
    [imageView addGestureRecognizer:pinchGesture];
    [pinchGesture release];

    UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGestureHandler:)];
    [imageView addGestureRecognizer:rotationGesture];
    [rotationGesture release];

    /*註釋panGesture的拖拽操作,否則划動和拖拽操作會產生混淆
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureHandler:)];
    [imageView addGestureRecognizer:panGesture];
    [panGesture release];
    */

    images = [[NSArray alloc] initWithObjects:@"iphone.png", @"mm.jpg", @"avatar.png", nil];

    //默認情況下是向右的手勢划動
    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureHandler:)];
    [imageView addGestureRecognizer:swipeGesture];
    [swipeGesture release];

    //定義向左的手勢划動
    UISwipeGestureRecognizer *swipeLeftGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureHandler:)];
    swipeLeftGesture.direction = UISwipeGestureRecognizerDirectionLeft;
    [imageView addGestureRecognizer:swipeLeftGesture];
    [swipeLeftGesture release];


    [super viewDidLoad];
}
- (IBAction)tapGestureHandler:(UIGestureRecognizer *)sender{
    if (sender.view.contentMode == UIViewContentModeScaleAspectFit) {
        sender.view.contentMode = UIViewContentModeScaleAspectFill;
    } else {
        sender.view.contentMode = UIViewContentModeScaleAspectFit;
    }

}
- (IBAction)pinchGestureHandler:(UIGestureRecognizer *)sender{
    CGFloat scaleValue = [(UIPinchGestureRecognizer *)sender scale];

    if (scaleValue > 1) {
        sender.view.transform = CGAffineTransformMakeScale(lastScaleValue + (scaleValue-1), lastScaleValue + (scaleValue-1));
    }else {
        sender.view.transform = CGAffineTransformMakeScale(lastScaleValue * scaleValue, lastScaleValue * scaleValue);
    } 

    if (sender.state == UIGestureRecognizerStateEnded) {
        if (scaleValue > 1) {
            lastScaleValue += (scaleValue - 1);
        }else {
            lastScaleValue += scaleValue;
        }
    }


}
- (IBAction)rotationGestureHandler:(UIGestureRecognizer *)sender{
    CGFloat rotation = [(UIRotationGestureRecognizer *)sender rotation];
    CGAffineTransform transform = CGAffineTransformMakeRotation(rotation + rotationValue);

    sender.view.transform = transform;

    if (sender.state == UIGestureRecognizerStateEnded) {
        rotationValue += rotation;
    }
}
- (IBAction)panGestureHandler:(UIGestureRecognizer *)sender{
    CGPoint position = [(UIPanGestureRecognizer *)sender translationInView:imageView];
    sender.view.transform = CGAffineTransformMakeTranslation(position.x + position.x, 
                                                             position.y + position.y);

    if (sender.state == UIGestureRecognizerStateEnded) {
        positionValue.x += position.x;
        positionValue.y += position.y;
    }
}
- (IBAction)swipeGestureHandler:(UIGestureRecognizer *)sender{
    UISwipeGestureRecognizerDirection direction =[(UISwipeGestureRecognizer *)sender direction];

    switch (direction) {
        case UISwipeGestureRecognizerDirectionUp:
            NSLog(@"向上划動");
            break;
            case UISwipeGestureRecognizerDirectionDown:
            NSLog(@"向下划動");
            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]];
}
- (void)dealloc {
    [images release];
    [imageView release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setImageView:nil];
    [super viewDidUnload];
}
@end

UILongPressGestureRecongnizer 檢測view上的長按操作

添加UIActionSheetDelegate協議

HomeViewController.h 

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

 #import "HomeViewController.h"

@interface HomeViewController ()
@end
@implementation HomeViewController
@synthesize imageView;
CGFloat lastScaleValue = 1;
CGFloat rotationValue;
CGPoint positionValue;
NSArray *images;
int imageIndex = 0;
- (void)viewDidLoad
{

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureHandler:)];
    tapGesture.numberOfTapsRequired = 2;
    [imageView addGestureRecognizer:tapGesture];
    [tapGesture release];

    UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchGestureHandler:)];
    [imageView addGestureRecognizer:pinchGesture];
    [pinchGesture release];

    UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGestureHandler:)];
    [imageView addGestureRecognizer:rotationGesture];
    [rotationGesture release];

    /*註釋panGesture的拖拽操作,否則划動和拖拽操作會產生混淆
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureHandler:)];
    [imageView addGestureRecognizer:panGesture];
    [panGesture release];
    */

    images = [[NSArray alloc] initWithObjects:@"iphone.png", @"mm.jpg", @"avatar.png", nil];

    //默認情況下是向右的手勢划動
    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureHandler:)];
    [imageView addGestureRecognizer:swipeGesture];
    [swipeGesture release];

    //定義向左的手勢划動
    UISwipeGestureRecognizer *swipeLeftGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureHandler:)];
    swipeLeftGesture.direction = UISwipeGestureRecognizerDirectionLeft;
    [imageView addGestureRecognizer:swipeLeftGesture];
    [swipeLeftGesture release];

    //開始長按的內容部分
    UILongPressGestureRecognizer *longpressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longpressGestureHandler:)];
    longpressGesture.minimumPressDuration = 1;
    longpressGesture.allowableMovement = 15;
    longpressGesture.numberOfTouchesRequired = 1;

    [imageView addGestureRecognizer:longpressGesture];
    [longpressGesture release];


    [super viewDidLoad];
}
- (IBAction)tapGestureHandler:(UIGestureRecognizer *)sender{
    if (sender.view.contentMode == UIViewContentModeScaleAspectFit) {
        sender.view.contentMode = UIViewContentModeScaleAspectFill;
    } else {
        sender.view.contentMode = UIViewContentModeScaleAspectFit;
    }

}
- (IBAction)pinchGestureHandler:(UIGestureRecognizer *)sender{
    CGFloat scaleValue = [(UIPinchGestureRecognizer *)sender scale];

    if (scaleValue > 1) {
        sender.view.transform = CGAffineTransformMakeScale(lastScaleValue + (scaleValue-1), lastScaleValue + (scaleValue-1));
    }else {
        sender.view.transform = CGAffineTransformMakeScale(lastScaleValue * scaleValue, lastScaleValue * scaleValue);
    } 

    if (sender.state == UIGestureRecognizerStateEnded) {
        if (scaleValue > 1) {
            lastScaleValue += (scaleValue - 1);
        }else {
            lastScaleValue += scaleValue;
        }
    }


}
- (IBAction)rotationGestureHandler:(UIGestureRecognizer *)sender{
    CGFloat rotation = [(UIRotationGestureRecognizer *)sender rotation];
    CGAffineTransform transform = CGAffineTransformMakeRotation(rotation + rotationValue);

    sender.view.transform = transform;

    if (sender.state == UIGestureRecognizerStateEnded) {
        rotationValue += rotation;
    }
}
- (IBAction)panGestureHandler:(UIGestureRecognizer *)sender{
    CGPoint position = [(UIPanGestureRecognizer *)sender translationInView:imageView];
    sender.view.transform = CGAffineTransformMakeTranslation(position.x + position.x, 
                                                             position.y + position.y);

    if (sender.state == UIGestureRecognizerStateEnded) {
        positionValue.x += position.x;
        positionValue.y += position.y;
    }
}
- (IBAction)swipeGestureHandler:(UIGestureRecognizer *)sender{
    UISwipeGestureRecognizerDirection direction =[(UISwipeGestureRecognizer *)sender direction];

    switch (direction) {
        case UISwipeGestureRecognizerDirectionUp:
            NSLog(@"向上划動");
            break;
            case UISwipeGestureRecognizerDirectionDown:
            NSLog(@"向下划動");
            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]];
}
- (IBAction)longpressGestureHandler:(UIGestureRecognizer *)sender{
    if ([(UILongPressGestureRecognizer *)sender state] == UIGestureRecognizerStateBegan) {
        UIActionSheet *actionSheet = [[UIActionSheet alloc] 
                                      initWithTitle:@"圖片操作" 
                                      delegate:self
                                      cancelButtonTitle:nil
                                      destructiveButtonTitle:nil
                                      otherButtonTitles:@"保存", @"複製", nil];

        [actionSheet showInView:self.view];
        [actionSheet release];
    }
}
- (void)dealloc {
    [images release];
    [imageView release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setImageView:nil];
    [super viewDidUnload];
}
@end

多點觸摸識別

有些時候,我們只想簡單地去識別用戶在屏幕上的操作,而不是隻針對某個特定的view,我們可以通過以下四個方法進行屏幕的觸摸的識別。

touchesBegan:withEvent:當有一個或多個手指觸摸屏幕時被觸發。

touchesMoved:withEvent:當有一個或多個手指在屏幕上移動時被觸發。

touchesEnded:withEvent:當有一個或多個手指離開屏幕時被觸發。

touchesCancelled:withEvent: 當系統事件(內存溢出警告或者來電)發生時,取消觸摸操作。

檢測單點觸摸

HomeViewController.h code:

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


HomeViewController.m code: 
 #import "HomeViewController.h"

@interface HomeViewController ()
@end
@implementation HomeViewController
@synthesize imageView;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NSSet *allTouches = [event allTouches];

    switch ([allTouches count]) {
        case 1:
        {
            UITouch *touch = [[allTouches allObjects] objectAtIndex:0];

            if ([touch tapCount] == 1 ) {
                imageView.contentMode = UIViewContentModeScaleAspectFit;
            }else if ([touch tapCount] == 2) {
                imageView.contentMode = UIViewContentModeScaleAspectFill;
            }
        }
            break;

        default:
            break;
    }
}
- (void)viewDidLoad
{
    [super viewDidLoad];
}
- (void)dealloc {
    [imageView release];
    [super dealloc];
}
@end

檢測多點觸摸

HomeViewController.h code:

#import <UIKit/UIKit.h>
@interface HomeViewController : UIViewController
@property (retain, nonatomic) IBOutlet UIImageView *imageView;
- (CGFloat)distance:(CGPoint)fromPoint toPoint:(CGPoint)toPoint;
@end
HomeViewController.m code:  

#import "HomeViewController.h"
@interface HomeViewController ()
@end
@implementation HomeViewController
@synthesize imageView;
CGFloat originalDistance;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NSSet *allTouches = [event allTouches];

    switch ([allTouches count]) {
        case 1:{
            UITouch *touch = [[allTouches allObjects] objectAtIndex:0];

            if ([touch tapCount] == 1 ) {
                imageView.contentMode = UIViewContentModeScaleAspectFit;
            }else if ([touch tapCount] == 2) {
                imageView.contentMode = UIViewContentModeScaleAspectFill;
            }
        }
            break;
        case 2:{

            UITouch *touch1 = [[allTouches allObjects] objectAtIndex:0];
            UITouch *touch2 = [[allTouches allObjects] objectAtIndex:1];

            CGPoint touch1PT = [touch1 locationInView:[self view]];
            CGPoint touch2PT = [touch2 locationInView:[self view]];

            NSLog(@"Touch1:%.0f, %.0f", touch1PT.x, touch1PT.y);
            NSLog(@"Touch2:%.0f, %.0f", touch2PT.x, touch2PT.y);

            originalDistance = [self distance:touch1PT toPoint:touch2PT];

        }break;

        default:
            break;
    }
}
- (CGFloat)distance:(CGPoint)fromPoint toPoint:(CGPoint)toPoint{
    float lengthX = fromPoint.x - toPoint.x;
    float lengthY = fromPoint.y - toPoint.y;
    return sqrt((lengthX * lengthX) + (lengthY * lengthY));
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    NSSet *allTouches = [event allTouches];

    switch ([allTouches count]) {
        case 1:
            break;
    case 2:{

        UITouch *touch1 = [[allTouches allObjects] objectAtIndex:0];
        UITouch *touch2 = [[allTouches allObjects] objectAtIndex:1];

        CGPoint touch1PT = [touch1 locationInView:[self view]];
        CGPoint touch2PT = [touch2 locationInView:[self view]];

        NSLog(@"Touch1:%.0f, %.0f", touch1PT.x, touch1PT.y);
        NSLog(@"Touch2:%.0f, %.0f", touch2PT.x, touch2PT.y);

        CGFloat currentDistance = [self distance:touch1PT toPoint:touch2PT];

        if (currentDistance > originalDistance) {
            imageView.frame = CGRectMake(imageView.frame.origin.x-2,imageView.frame.origin.y-2,
                                         imageView.frame.size.width+4, imageView.frame.size.height+4);
        }else {
            imageView.frame = CGRectMake(imageView.frame.origin.x+2, imageView.frame.origin.y+2,
                                         imageView.frame.size.width-4, imageView.frame.size.height-4);
        }
        originalDistance = currentDistance;
    }break;

        default:
            break;
    }
}
- (void)viewDidLoad
{
    [super viewDidLoad];
}
- (void)dealloc {
    [imageView release];
    [super dealloc];
}
@end
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章