【问题标题】:Circular UIButtons Getting Distorted on Rotation Animation Due to Resize由于调整大小,圆形 UIButtons 在旋转动画上失真
【发布时间】:2016-06-15 21:09:56
【问题描述】:

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

- (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

但似乎为了设置半径以通过该方法工作,我必须以编程方式设置按钮的大小,而不是使用自动布局。有人对处理这个有什么神奇的建议吗?我肯定不想像 Apple 那样轮换,但不幸的是,这个决定不是我的。

【问题讨论】:

    标签: ios objective-c iphone uibutton autolayout


    【解决方案1】:

    哇,这比我想象的要难。幸运的是,WWDC 现在正在进行中,我能够从 Interface Builder 实验室获得解决方案。他的解决方案是继承 UIButton 并覆盖 drawRect: 方法。所以这是您应该在 CircleButton 类中拥有的唯一方法。我发现的一个问题是 lineWidth 属性在被笔尖初始化之前没有设置。我覆盖了 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];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多