objective-c – 单击时更改按钮背景颜色
发布时间:2020-12-14 19:22:41 所属栏目:百科 来源:网络整理
导读:我创建了一个按钮,我希望在用户点击时更改其颜色.我看到了选项“setimagebackground … forstate ……”,但没有其他选项可以改变背景颜色.我在viewdidload中更改了y按钮的值,当我使用“settint color:[uicolor redcolor]”时,没有任何反应. 我该怎么解决?
我创建了一个按钮,我希望在用户点击时更改其颜色.我看到了选项“setimagebackground … forstate ……”,但没有其他选项可以改变背景颜色.我在viewdidload中更改了y按钮的值,当我使用“settint color:[uicolor redcolor]”时,没有任何反应.
我该怎么解决? 这是我的代码: button1.layer.borderColor = [[UIColor purpleColor]CGColor]; button1.layer.cornerRadius = 8.0f; button1.layer.masksToBounds = YES; button1.layer.borderWidth = 1.0f; [button1.tintColor = [UIColor redColor]CGColor]; [button1 setTintColor:[UIColor blueColor]]; [button1 setTitleColor:[UIColor cyanColor] forState:UIControlStateNormal]; [button1 setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted]; [button1 setTintColor:[UIColor redColor]]; 谢谢! 解决方法
有了图片:
[button1 setBackgroundImage:[UIImage imageNamed:@"redButton.png"]]; 没有图片: [button1 setBackgroundColor:[UIColor redColor] ]; 有了这个你可以得到圆形边框: CALayer * layer = [button1 layer]; [layer setCornerRadius:8.0f]; [layer setMasksToBounds:YES]; [layer setBorderWidth:1.0f]; [layer setBorderColor:[[UIColor whiteColor] CGColor]]; button1.backgroundColor = [UIColor redColor]; 对于选定的状态子类UIButton,以这种方式: 在.h文件中: @interface MyButton : UIButton { @private NSMutableDictionary *backgroundStates; @public } - (void) setBackgroundColor:(UIColor *) _backgroundColor forState:(UIControlState) _state; - (UIColor*) backgroundColorForState:(UIControlState) _state; @end 在.m文件中: #import "MyButton.h" @implementation MyButton - (void) setBackgroundColor:(UIColor *) _backgroundColor forState:(UIControlState) _state { if (backgroundStates == nil) backgroundStates = [[NSMutableDictionary alloc] init]; [backgroundStates setObject:_backgroundColor forKey:[NSNumber numberWithInt:_state]]; if (self.backgroundColor == nil) [self setBackgroundColor:_backgroundColor]; } - (UIColor*) backgroundColorForState:(UIControlState) _state { return [backgroundStates objectForKey:[NSNumber numberWithInt:_state]]; } #pragma mark - #pragma mark Touches - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesBegan:touches withEvent:event]; UIColor *selectedColor = [backgroundStates objectForKey:[NSNumber numberWithInt:UIControlStateHighlighted]]; if (selectedColor) { CATransition *animation = [CATransition animation]; [animation setType:kCATransitionFade]; [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaSEOut]]; [self.layer addAnimation:animation forKey:@"EaSEOut"]; self.backgroundColor = selectedColor; } } - (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesCancelled:touches withEvent:event]; UIColor *normalColor = [backgroundStates objectForKey:[NSNumber numberWithInt:UIControlStateNormal]]; if (normalColor) { CATransition *animation = [CATransition animation]; [animation setType:kCATransitionFade]; [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaSEOut]]; [self.layer addAnimation:animation forKey:@"EaSEOut"]; self.backgroundColor = normalColor; } } - (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesEnded:touches withEvent:event]; UIColor *normalColor = [backgroundStates objectForKey:[NSNumber numberWithInt:UIControlStateNormal]]; if (normalColor) { CATransition *animation = [CATransition animation]; [animation setType:kCATransitionFade]; [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaSEOut]]; [self.layer addAnimation:animation forKey:@"EaSEOut"]; self.backgroundColor = normalColor; } } - (void) dealloc { [backgroundStates release]; [super dealloc]; } @end 然后在ViewController中(记住要包含您的自定义类),您可以这样设置颜色: [button1 setBackgroundColor:[UIColor colorWithRed:0.8 green:0.7 blue:0.6 alpha:1.0] forState:UIControlStateHighlighted]; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |