我将 UIImageView 子类化为允许我旋转图像,或者使用用户的手指拖动图像,或者使用滑块来设置图像旋转。
这一切在最初使用时都可以正常工作,但是如果用户旋转图像然后重新定位设备,那么图像大小会被严重搞砸 - 如果用户顺时针旋转图像,那么图像会变得很大,逆时针图像变小了。
如果我将 UIImageView 的 autoresizingMask 设置为 none,那么问题就会消失,但是如果我这样做,那么我将需要在设备旋转时手动移动/重新调整视图的大小,我不希望这样做。
以下代码完成了大部分工作:
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{
self.spinInProgress = NO;
if (!self.spinnable || [[event allTouches] count]>1) {
[super touchesBegan:touches withEvent:event];
return;
}
UITouch *touch = (UITouch*)[touches anyObject];
//check that touch is within radius of the circle
float radius = MIN(self.frame.size.height, self.frame.size.width);
radius = radius / 2;
CGPoint centre = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
CGPoint posInView = [touch locationInView:self];
float x = posInView.x;
float y = posInView.y;
float centre_x = centre.x;
float centre_y = centre.y;
if ((powf(x - centre_x, 2) + powf(y - centre_y, 2)) > powf(radius, 2))
return; //not within the circle..
//so now doing the work
self.spinInProgress = YES;
//stop the spinning if it is doing so
if (spinTimer!=nil && [spinTimer isValid]){
[spinTimer invalidate];
spinTimer = nil;
}
//store the current touch as the previous touch position for now for future calculation when touch moves
CGPoint previousPosition = [touch locationInView:self.superview];
CGPoint lowerLeft = self.frame.origin;
CGSize size = self.frame.size;
//find the centre of the image (really should cache this!)
centreX = (size.width/2) + lowerLeft.x;
centreY = (size.height/2) + lowerLeft.y;
startX = previousPosition.x;
startY = previousPosition.y;
//work out the angle of the current touch
float startdx = previousPosition.x - centreX;
float startdy = previousPosition.y - centreY;
startAngle = atan2(startdx, startdy) *180/M_PI;
startAngle += self.currentAngle;
[super touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if (!self.spinInProgress) {
[super touchesMoved:touches withEvent:event];
return;
}
UITouch *touch = (UITouch*)[touches anyObject];
//work out the current position of the touch
CGPoint currentPosition = [touch locationInView:self.superview];
//calculate the angle
float dx = currentPosition.x - centreX;
float dy = currentPosition.y - centreY;
float newangle = atan2(dx,dy) * 180/ M_PI;
float rotateangle = startAngle - newangle;
//float movedAngle = rotateangle - currentAngle;
if (rotateangle > self.currentAngle){
clockSpin = YES;
} else {
clockSpin = NO;
}
if (rotateangle>360)
rotateangle = rotateangle-360;
if (rotateangle<0)
rotateangle = fabs(rotateangle);
self.currentAngle = rotateangle;
lastMove = [event timestamp];
[super touchesMoved:touches withEvent:event];
}
-(void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
[self stop:self];
self.spinInProgress = NO;
[super touchesEnded:touches withEvent:event];
}
-(void)setCurrentAngle:(float)currentAngle
{
[self willChangeValueForKey:@"currentAngle"];
__currentAngle = currentAngle;
[self didChangeValueForKey:@"currentAngle"];
CGAffineTransform trans = CGAffineTransformMakeRotation(self.currentAngle*DEGREES_TO_RADIANS);
self.transform = trans;
}
我认为最重要的一点是 setCurrentAngle ,因为它可以完成工作,但我认为我应该发布其余部分以提供一些背景信息。
您可以提供的任何帮助将不胜感激!
谢谢
理查德