由于调整大小,圆形UIButton因旋转动画而变形

时间:2016-06-15 21:09:56

标签: ios objective-c iphone uibutton autolayout

我在iPhone上有这个10键盘,它与iPhone解锁屏幕的屏幕基本相同,只有我的旋转,调整按钮的大小以适应屏幕。问题是旋转设备的动画会扭曲圆形,因为它们已经改变了大小,但是cornerRadius在完成动画之前是相同的。旋转动画完成后,按钮会再次设置半径,从而使它们成圆形。我不知道如何使UIButton始终是圆的,特别是在旋转动画期间。这基本上就是我所拥有的:

- (void)viewDidLayoutSubviews {
    [self setupButtons];
}

- (void)setupButtons {
    for (UIButton *button in self.touchPadButtons) {
        [self roundifyButton:button withRadius:radius];
    }
}

- (void)roundifyButton:(UIButton *)button {
    NSInteger height = button.frame.size.height;
    radius = height/2;
    button.layer.cornerRadius = radius;
    button.layer.borderWidth = .6f;
    button.layer.borderColor = [UIColor whiteColor].CGColor;
    button.clipsToBounds = YES;
}

我尝试过使用:

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator

但似乎为了将半径设置为从该方法起作用,我必须以编程方式设置我的按钮大小而不是使用autolayout。有没有人对处理这个有什么神奇的建议?我很乐意不像苹果那样轮换,但不幸的是,这个决定不是我的决定。

1 个答案:

答案 0 :(得分:0)

哇,这比我想象的要难。幸运的是,WWDC正在进行中,我能够从Interface Builder实验室获得解决方案。他的解决方案是继承UIButton并覆盖drawRect:方法。所以这是CircleButton类中唯一的方法。我发现的一个问题是linenidth属性在由nib初始化之前不会被设置。我覆盖了init方法并设置了一个默认值,但是当nib初始化按钮时它没有第一次被击中。所以我不得不在drawRect:方法中添加默认值。我希望这可以帮助那些需要可以调整大小的圆形UIButton的人。

- (void)drawRect:(CGRect)rect {
    [[UIColor whiteColor] set];
    if (!self.lineWidth) {
        self.lineWidth = 0.75;
    }

    CGRect bounds = [self bounds];
    CGRect circleRect = CGRectMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds), 0, 0);

    CGFloat radius = MIN(bounds.size.width, bounds.size.height) / 2.0;
    circleRect = CGRectInset(circleRect, -radius, -radius);

    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectInset(circleRect, self.lineWidth / 2.0, self.lineWidth / 2.0)];

    [path setLineWidth:self.lineWidth];
    [path stroke];
}

如果您想要为按钮设置动画,请单击iPhone锁定屏幕的方式,您需要将其添加到CircleButton类。否则只会突出显示titleLabel。

- (void)drawRect:(CGRect)rect {
    [[UIColor whiteColor] set];
    if (!self.lineWidth) {
        self.lineWidth = 0.75;
    }

    CGRect bounds = [self bounds];
    CGRect circleRect = CGRectMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds), 0, 0);

    CGFloat radius = MIN(bounds.size.width, bounds.size.height) / 2.0;
    circleRect = CGRectInset(circleRect, -radius, -radius);
    self.layer.cornerRadius = radius;
    self.clipsToBounds = YES;

    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectInset(circleRect, self.lineWidth / 2.0, self.lineWidth / 2.0)];

    [path setLineWidth:self.lineWidth];
    [path stroke];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    // highlight button on click
    self.backgroundColor = [UIColor lightGrayColor];
    [super touchesBegan:touches withEvent:event];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    // remove highlight
    self.backgroundColor = [UIColor clearColor];
}