0

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

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

在我的布局子视图中,我创建了一个标签和贝塞尔路径,每个标签仅在标签不存在时创建,并且使用视图的 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];
    }
}
4

1 回答 1

0

要么你使用不喜欢变换的自动布局,要么你updatePosition不止一次调用,因此你正在改变frame已经变换的视图,这会给你带来很多痛苦。添加

self.transform = CGAffineTransformIdentity;

作为 的第一行updatePosition

于 2013-11-09T20:28:21.400 回答