由于现在要做的一个应用里有一个旋钮,所以昨天在网上找了一天,终于找到了一份单手指滑动旋转的demo,但是它有几个问题。
直接运行的话,打不开,说什么所选设备不支持什么的。
于是我就自己建了一个工程,然后把代码拷过去,运行,发现所选图片并不是绕着一个中心点在旋转,而是有点偏移,找了好久,问了几个人,发现是在Xib里勾选了“Use Auto Layout”,取消自动布局就行了!!!
下面是详细代码:
// KTOneFingerRotationGestureRecognizer.h
#import <UIKit/UIKit.h>
@interface KTOneFingerRotationGestureRecognizer : UIGestureRecognizer
{
}
/**
The rotation of the gesture in radians since its last change.
*/
@property (nonatomic, assign) CGFloat rotation;
@end
// KTOneFingerRotationGestureRecognizer.m
#import "KTOneFingerRotationGestureRecognizer.h"
#import <UIKit/UIGestureRecognizerSubclass.h>
@implementation KTOneFingerRotationGestureRecognizer
@synthesize rotation = rotation_;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// Fail when more than 1 finger detected.
if ([[event touchesForGestureRecognizer:self] count] > 1) {
[self setState:UIGestureRecognizerStateFailed];
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([self state] == UIGestureRecognizerStatePossible) {
[self setState:UIGestureRecognizerStateBegan];
} else {
[self setState:UIGestureRecognizerStateChanged];
}
// We can look at any touch object since we know we
// have only 1. If there were more than 1 then
// touchesBegan:withEvent: would have failed the recognizer.
UITouch *touch = [touches anyObject];
// To rotate with one finger, we simulate a second finger.
// The second figure is on the opposite side of the virtual
// circle that represents the rotation gesture.
UIView *view = [self view];
CGPoint center = CGPointMake(CGRectGetMidX([view bounds]), CGRectGetMidY([view bounds]));
CGPoint currentTouchPoint = [touch locationInView:view];
CGPoint previousTouchPoint = [touch previousLocationInView:view];
CGFloat angleInRadians = atan2f(currentTouchPoint.y - center.y, currentTouchPoint.x - center.x) - atan2f(previousTouchPoint.y - center.y, previousTouchPoint.x - center.x);
[self setRotation:angleInRadians];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// Perform final check to make sure a tap was not misinterpreted.
if ([self state] == UIGestureRecognizerStateChanged) {
[self setState:UIGestureRecognizerStateEnded];
} else {
[self setState:UIGestureRecognizerStateFailed];
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[self setState:UIGestureRecognizerStateFailed];
}
@end
上面这个类是继承 UIGestureRecognizer的,接下来就来实现旋转。
// RootViewController.h
#import <UIKit/UIKit.h>
#import "KTOneFingerRotationGestureRecognizer.h"
@interface RootViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@end
// RootViewController.m
#import "RootViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.imageView.userInteractionEnabled = YES; //允许界面交互
[self addRotationGestureToView:self.imageView];
}
- (void)addRotationGestureToView:(UIView *)view
{
KTOneFingerRotationGestureRecognizer *rotation = [[KTOneFingerRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotating:)];
[view addGestureRecognizer:rotation];
}
- (void)rotating:(KTOneFingerRotationGestureRecognizer *)recognizer
{
UIView *view = [recognizer view];
[view setTransform:CGAffineTransformRotate([view transform], [recognizer rotation])];
// recognizer.rotation = 0;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
不知道为什么不能直接上传文件,不然我就把我的写的demo传上去了。