【发布时间】:2011-03-15 03:06:16
【问题描述】:
我真的被旋转视图时遇到的问题难住了。我试图让几个视图围绕任意点旋转(如触摸)。
首先,没有充分解释(相信我的话)我只想说,由于我的视图布局和我需要完成的复杂性,“作弊”方法,因为它有时是调用,在其中添加所有需要旋转到更大视图并旋转该视图的视图,对我不起作用。
我正在更改图层的锚点,以便我的视图将围绕双击点旋转。然而,一些我无法弄清楚的事情一直让我陷入困境。当我将它旋转一次(比如 90º),然后再次旋转 90º,而不是以 180º(您所期望的)结束时,我以 270º(或其他一些奇怪的数字)结束。
我将尝试发布足够多的代码,以便您可以看到我在说什么(希望能帮助我)。
我在 .h 中声明了一个实例变量
UIView *theView;
这是 .m(您需要导入 QuartzCore 框架)
- (void)viewDidLoad {
[super viewDidLoad];
theView = [[UIView alloc]initWithFrame:CGRectMake(140, 40, 40, 40)];
theView.backgroundColor = [UIColor blueColor];
[self.view addSubview:theView];
}
#define DegreesToRadians(x) ((x) * M_PI / 180.0)
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if ([touch tapCount]==2) {
CGFloat xValue = ((touchLocation.x - theView.frame.origin.x)/theView.frame.size.width);
CGFloat yValue = ((touchLocation.y - theView.frame.origin.y)/theView.frame.size.height);
CGRect priorRect = theView.frame;
theView.layer.anchorPoint = CGPointMake(xValue, yValue);
theView.frame = priorRect;
static float rotationValue = 0.0f;
rotationValue += 90.0f;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
theView.transform = CGAffineTransformRotate(theView.transform, DegreesToRadians(rotationValue));
[UIView commitAnimations];
}
}
我已经尝试过所有我能想到的不同配置,所以这个例子就是我拼凑起来的,因为它看起来很简洁。无论如何,请帮助我!!!
【问题讨论】:
标签: iphone objective-c cocoa quartz-graphics