我试图将图像添加到可以旋转的 ViewController。
问题是,一旦我尝试旋转可移动对象,对象就会移动到它的初始化位置,到原点 x,y 并在那里旋转,而不是在原地旋转。我的问题是如何防止这样做,有没有办法在运动结束后立即设置对象位置?
#import "MovableImageView.h"
@implementation MovableImageView
-(id)initWithImage:(UIImage *)image
{
self = [super initWithImage:image];
if (self) {
UIRotationGestureRecognizer *rotationGestureRecognizer= [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(handleRotations:)];
[self addGestureRecognizer:rotationGestureRecognizer];
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
}
-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
float deltaX = [[touches anyObject] locationInView:self].x - [[touches anyObject] previousLocationInView:self].x;
float deltaY = [[touches anyObject] locationInView:self].y - [[touches anyObject] previousLocationInView:self].y;
self.transform = CGAffineTransformTranslate(self.transform, deltaX, deltaY);
}
-(void) handleRotations: (UIRotationGestureRecognizer *) paramSender
{
self.transform= CGAffineTransformMakeRotation(self.rotationAngleInRadians + paramSender.rotation);
if (paramSender.state == UIGestureRecognizerStateEnded) {
self.rotationAngleInRadians += paramSender.rotation;
}
}
@end