UIButton's imageView property and hidden/alpha value
我有一个 UIButton 在某些情况下应该显示一个活动指示器而不是图像。我目前所做的是将按钮的 imageView 的隐藏属性设置为 YES 并返回。我还尝试将 alpha 值设置为 0.0f 并返回 1.0f。
这一直有效,直到按钮的状态发生变化。这会重置 imageView 的属性并导致 hidden == NO 和 alpha == 1.0f。
有没有人做过类似的事情或知道如何隐藏按钮的 imageView 而其余部分保持可见?
您可以通过使用 view\\'s layer 的 transform 属性来实现这一点,即
隐藏
快速代码
1 | button.imageView?.layer.transform = CATransform3DMakeScale(0.0, 0.0, 0.0) |
objective-C 代码
1 | button.imageView.layer.transform = CATransform3DMakeScale(0, 0, 0); |
取消隐藏
1 | button.imageView?.layer.transform = CATransform3DIdentity |
objective-C 代码
1 | button.imageView.layer.transform = CATransform3DIdentity |
这很简单。
按钮有不同的状态。只需为选定状态设置零图像,为正常状态设置图像。当您想显示活动指示器时,将按钮状态设置为选中并将指示器添加为按钮子视图。
1 2 3 4 5 6 7 8 9 10 11 12 | - (void)initializeButton { button = [[UIButton alloc] init]; [button addTarget:self action:@selector(buttonDidClick:) forControlEvents:UIControlEventTouchUpInside]; [button setImage:[[UIImage alloc] init] forState:UIControlStateSelected]; //zero image [button setImage:yourImage forState:UIControlStateSelected]; //your image } - (void)buttonDidClick:(UIButton *)button { button.selected = YES; /* Add yor activity indicator with some view tag or save reference to it */ } |
当你的活动完成时调用下一个方法
1 2 3 4 | - (void)finishActivityWithButton:(UIButton *)button { button.selected = NO; /* Remove your activity indicator from button */ } |
如果您使用的是模板图像,请将色调颜色设置为 [UIColor clearColor](应该也适用于其他人,没有尝试)
1 | [[button imageView] setTintColor:[UIColor clearColor]]; |
您可以通过在想要隐藏图像时从按钮中删除图像并在需要显示图像时将图像分配回按钮来实现此目的。
当你想隐藏图像时,可以这样写:
1 | [yourButton setImage:nil forState:UIControlStateNormal]; |
以及当你想要显示图像时:
1 | [yourButton setImage:yourImage forState:UIControlStateNormal]; |
这里
改变变换或选择状态会导致另一个问题,最好的方法是使用category:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | const char kNormalImage; const char kSelectImage; const char kDisableImage; @interface UIButton () @property (nonatomic, strong) UIImage *normalImage; @property (nonatomic, strong) UIImage *selectImage; @property (nonatomic, strong) UIImage *disableImage; @end @implementation UIButton (Hidden) - (void)setImageViewHidden:(BOOL)hidden { if (hidden) { self.normalImage = [self imageForState:UIControlStateNormal]; self.selectImage = [self imageForState:UIControlStateSelected]; self.disableImage = [self imageForState:UIControlStateDisabled]; UIGraphicsBeginImageContextWithOptions(self.imageView.bounds.size, NO, 0.0); UIImage *blank = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); [self setImage:blank forState:UIControlStateNormal]; [self setImage:blank forState:UIControlStateSelected]; [self setImage:blank forState:UIControlStateDisabled]; } else { [self setImage:self.normalImage forState:UIControlStateNormal]; [self setImage:self.selectImage forState:UIControlStateSelected]; [self setImage:self.disableImage forState:UIControlStateDisabled]; } } - (UIImage *)normalImage { return objc_getAssociatedObject(self, &kNormalImage); } - (void)setNormalImage:(UIImage *)normalImage { objc_setAssociatedObject(self, &kNormalImage, normalImage, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } - (UIImage *)selectImage { return objc_getAssociatedObject(self, &kSelectImage); } - (void)setSelectImage:(UIImage *)selectImage { objc_setAssociatedObject(self, &kSelectImage, selectImage, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } - (UIImage *)disableImage { return objc_getAssociatedObject(self, &kDisableImage); } - (void)setDisableImage:(UIImage *)disableImage { objc_setAssociatedObject(self, &kDisableImage, disableImage, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } @end |