我真的難以擺脫旋轉視圖時遇到的問題。我試圖讓幾個視圖圍繞任意點旋轉(如觸摸)。我只想說,由於視圖佈局的複雜性以及我需要完成的工作,「欺騙」方法就像它一樣,有時稱爲,將所有需要旋轉到更大視圖並旋轉該視圖的視圖添加到其中,這對我而言不起作用。如何多次旋轉錨點附近的視圖
我正在改變圖層的錨點,以便我的視圖將圍繞雙擊旋轉。然而,我無法弄清楚的事情總是讓我陷入循環。當我旋轉一次(比如說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];
}
}
我與每一個不同的嘗試這樣做我可能想到的配置,所以這個例子就是我扔在一起,因爲它看起來很簡潔。無論如何,請幫助我!
我閱讀了提供的鏈接中的所有帖子。這仍然沒有解決我遇到的問題。沒有人嘗試過提供的代碼?你可以很快看到發生了什麼事。 – daveMac 2011-03-15 14:16:56
測試了代碼,並看到錨點沒有正確設置。添加了一個處理該問題的鏈接。這個「怪異」的角度問題在一定程度上依然存在,並在第二個環節加以解決。 – 2011-03-15 14:57:47