我应该如何与UIView的几何体进行交互,而忽略所应用的任何变换?

时间:2008-11-18 01:02:31

标签: iphone cocoa-touch

我认为我希望用户围绕其中心旋转,通过点击并按住某个地方,然后将他们的手指四处移动。

我已经解决了所有的几何问题;我所做的是将相对于中心的初始触摸角度存储为offsetAngle,然后我的touchesMoved方法如下所示:

- (void) touchesMoved: (NSSet *)touches withEvent:(UIEvent *)event {
    self.transform = CGAffineTransformMakeRotation(0);
    CGPoint location = [[touches anyObject] locationInView: self];    
    CGPoint actualCenter = [self convertPoint: self.center fromView: self.superview];    
    CGPoint relativeTouch = [MathHelper translatePoint:location relativeTo: actualCenter];
    CGPoint zero = CGPointMake(0, 0);
    float angle = [MathHelper getAngleForPoint:zero andPoint:relativeTouch];
    self.transform = CGAffineTransformMakeRotation(offsetAngle-angle);
}

丑陋的位是第一行,我必须恢复旋转才能在视图中获得正确的事件位置。如果我不这样做,那么由于视图正在旋转,所以位置会跳到整个地方,因为视图正在旋转......

另一个问题是,当视图应用了变换时,您想要操纵视图的帧(例如,将其向下移动):

- (IBAction)toggleSettingsView {
    BOOL state = [settingsSwitch isOn];
    float height = optionsView.bounds.size.height;
    CGRect polygonFrame = polygonView.frame; 

    [UIView beginAnimations:@"advancedAnimations" context:nil]; 
    [UIView setAnimationDuration:0.3]; 
    if (state) {
        optionsView.alpha = 1.0; 
        polygonFrame.origin.y += height; 
    } else {
        optionsView.alpha = 0.0; 
        polygonFrame.origin.y -= height; 
    }
    polygonView.frame = polygonFrame;
    [UIView commitAnimations]; 

}

这严重扭曲了观点。

我必须提到CGTransform和CALayer变换都有相同的效果。

这闻起来我做错了什么,但我不知道自己应该做些什么。

2 个答案:

答案 0 :(得分:2)

我会使用superview的坐标系,因为它不受旋转的影响:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint location = [[touches anyObject] locationInView:self.superview];
    CGPoint relativeTouch = [MathHelper translatePoint:location relativeTo:self.center];
    CGPoint zero = CGPointMake(0, 0);
    float angle = [MathHelper getAngleForPoint:zero andPoint:relativeTouch];
    self.transform = CGAffineTransformMakeRotation(offsetAngle-angle);
}

答案 1 :(得分:0)

只需将原始变换保存在ivar中并将其称为一天。