CGAffineTransformMakeRotation UIView腐败

时间:2013-11-09 14:47:36

标签: ios iphone objective-c uiview cgaffinetransform

我在旋转UIView时遇到了一个问题。

我有一个名为'updatePositions'的方法,它将视图的当前帧设置为superview的左侧或右侧,然后调用旋转函数来旋转整个UIView。但是,当我调用它时,框架设置正常,但旋转完全破坏了视图。

在我的布局子视图中,我创建了一个标签和bezier路径,每个路径只在标签不存在且使用视图的bounds属性布局时才会创建。

我是否在错误的地方调用CGAffineTransformMakeRotation?

- (void)updatePosition
{
    // If the join overview orientates to north set position
    if (self.joinOverview.doesOrientateToNorth)
        // If the approach track is not south west place the north indicator in the lower left of the diagram
        // if approach track is south west place north indicator in lower right
        if (self.joinOverview.dataItem.approachTrack != VDApproachTrackSouthWest)
            [self setFrame:CGRectMake(-self.superview.frame.origin.x, self.superview.frame.size.height - VIEW_SIZE, VIEW_SIZE, VIEW_SIZE)];
        else
            [self setFrame:CGRectMake(self.superview.frame.size.width - VIEW_SIZE, self.superview.frame.size.height - VIEW_SIZE, VIEW_SIZE, VIEW_SIZE)];
    }
    else // If the join overview orientates to the approach track set rotation and position
    {
        // Set the position to the lower left of the view
        [self setFrame:CGRectMake(-self.superview.frame.origin.x, self.superview.frame.size.height - VIEW_SIZE, VIEW_SIZE, VIEW_SIZE)];

        // Rotate
        self.transform = CGAffineTransformMakeRotation(DegreesToRadians(50));
    }
}


- (void)layoutSubviews
{
    // Super
    [super layoutSubviews];

    // set background colour to transparent
    self.backgroundColor = [UIColor clearColor];

    if (!self.northSymbol) {
    // Add 'N' symbol label
    self.northSymbol = [[UILabel alloc]initWithFrame:CGRectMake(0, self.bounds.size.height / 2, self.bounds.size.width, self.bounds.size.height / 2)];
    [self.northSymbol setText:@"N"];
    [self.northSymbol setTextAlignment:NSTextAlignmentCenter];
    [self.northSymbol setFont:[UIFont boldSystemFontOfSize:18]];
    [self addSubview:self.northSymbol];

    self.arrow = [UIBezierPath bezierPathWithThreePointArrowFromPoint:CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2) 
     toPoint:CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 20) 
     headWidth:self.bounds.size.width / 2.5];
    }
}

1 个答案:

答案 0 :(得分:0)

您使用的是不喜欢变换的自动布局,或者您多次调用updatePosition,因此您正在更改已转换视图的frame,这将导致你很痛苦。添加

self.transform = CGAffineTransformIdentity;

作为updatePosition的第一行。

相关问题