自定义手势

首先,在CGPointUtils中定义一些方法。

 

头文件:

 

#import <CoreGraphics/CoreGraphics.h>

#define degreesToRadian(x)  (M_PI * x / 180.0)
#define radiansToDegrees(x) (180.0 * x / M_PI)

#define HWX(x) (int)(x - screen.size.height / 2)
#define HWY(x) (int)(screen.size.width - x)

CGFloat distanceBetweenPoints(CGPoint first, CGPoint second);
CGFloat angleBetweenPoints(CGPoint first, CGPoint second);
CGFloat angleBetweenLines(CGPoint line1Start, CGPoint line1End, CGPoint line2Start, CGPoint lin2End);

 

实现文件:

 

#include "CGPointUtils.h"
#include <math.h>

CGFloat distanceBetweenPoints(CGPoint first, CGPoint second) {
    CGFloat deltaX = second.x - first.x;
    CGFloat deltaY = second.y - first.y;
    return sqrt(deltaX * deltaX + deltaY * deltaY);
}

CGFloat angleBetweenPoints(CGPoint first, CGPoint second) {
    CGFloat height = second.y - first.y;
    CGFloat width = first.x - second.x;
    CGFloat rads = atan(height / width);
    return radiansToDegrees(rads);
}

CGFloat angleBetweenLines(CGPoint line1Start, CGPoint line1End, CGPoint line2Start, CGPoint line2End) {
    CGFloat a = line1End.x - line1Start.x;
    CGFloat b = line1End.y - line1Start.y;
    CGFloat c = line2End.x - line2Start.x;
    CGFloat d = line2End.y - line2Start.y;
    CGFloat rads = acos(((a * c) + (b * d)) / ((sqrt(a * a + b * b)) * (sqrt(c * c + d * d))));
    return radiansToDegrees(rads);
}

 

然后,看一下具体的示例。

 

头文件:

 

#import <UIKit/UIKit.h>
#import "CGPointUtils.h"

@interface CheckPleaseViewController : UIViewController {
	IBOutlet UILabel *label;
	CGPoint  lastPreviousPoint;
	CGPoint  lastCurrentPoint;
	CGFloat lineLengthSoFar;
}

@property (nonatomic, retain) IBOutlet UILabel *label;

- (void)eraseLabel;

@end

 

实现文件:

 

#define kMinimumCheckMarkAngle 50
#define kMaximumCheckMarkAngle 135
#define kMinimumCheckMarkLength 10

#import "CheckPleaseViewController.h"

@implementation CheckPleaseViewController
@synthesize label;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	UITouch *touch = [touches anyObject];
	CGPoint point = [touch locationInView:self.view];
	lastPreviousPoint = point;
	lastCurrentPoint = point;
	lineLengthSoFar = 0.0f;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
	UITouch *touch = [touches anyObject];
	CGPoint previousPoint = [touch previousLocationInView:self.view];
	CGPoint currentPoint = [touch locationInView:self.view];
	CGFloat angle = angleBetweenLines(lastPreviousPoint, lastCurrentPoint, previousPoint, currentPoint);
	
	if((angle >= kMinimumCheckMarkAngle) && (angle <= kMaximumCheckMarkAngle) && (lineLengthSoFar > kMinimumCheckMarkLength)){
		label.text = @"Checkmark";
		[self performSelector : @selector(eraseLabel) withObject:nil afterDelay:1.6f];
	}
	
	lineLengthSoFar += distanceBetweenPoints(previousPoint, currentPoint);
	lastPreviousPoint = previousPoint;
	lastCurrentPoint =  currentPoint;
}

- (void)eraseLabel {
    label.text = @"";
}

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

@end

 

你可能感兴趣的:(ios,iPhone,自定义手势)