IOS之高级按钮

这个是我自己写的稍微漂亮的button的方法,希望大家喜欢。

在viewcontroller.h代码

[html]  view plain copy
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController  
  4.   
  5. @property(nonatomic, strong) IBOutlet UIButton *startGameButton;  
  6. @property(nonatomic, strong) IBOutlet UIButton *multiplayerButton;  
  7. @property(nonatomic, strong) IBOutlet UIButton *highScoresButton;  
  8. @property(nonatomic, strong) IBOutlet UIButton *creditsButton;  
  9.   
  10. @end  


在viewcontroller.m 代码

[html]  view plain copy
  1. #import "ViewController.h"  
  2. #import <QuartzCore/QuartzCore.h>  
  3.   
  4. @interface ViewController ()  
  5.   
  6. @end  
  7.   
  8. @implementation ViewController  
  9.   
  10. @synthesize startGameButton, multiplayerButton, highScoresButton, creditsButton;  
  11.   
  12. - (void)viewDidLoad  
  13. {  
  14.     [super viewDidLoad];  
  15.     // Do any additional setup after loading the view, typically from a nib.  
  16.       
  17.     NSArray *buttons = [NSArray arrayWithObjects: self.startGameButton, self.multiplayerButton, self.highScoresButton,self.creditsButton,nil];  
  18.       
  19.       
  20.     for(UIButton *btn in buttons)  
  21.     {  
  22.           
  23.         // BEGIN WORKING FROM TUTORIAL HERE  
  24.           
  25.         // Set the button Text Color  
  26.         [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];  
  27.         [btn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];  
  28.           
  29.         // Set default backgrond color  
  30.         [btn setBackgroundColor:[UIColor blackColor]];  
  31.           
  32.         // Add Custom Font  
  33.         [[btn titleLabel] setFont:[UIFont fontWithName:@"Knewave" size:18.0f]];  
  34.          
  35.           
  36.         // Draw a custom gradient  
  37.         CAGradientLayer *btnGradient = [CAGradientLayer layer];  
  38.         btnGradient.frame = btn.bounds;  
  39.         btnGradient.colors = [NSArray arrayWithObjects:  
  40.          (id)[[UIColor colorWithRed:102.0f / 255.0f green:102.0f / 255.0f blue:102.0f / 255.0f alpha:1.0f] CGColor],  
  41.          (id)[[UIColor colorWithRed:51.0f / 255.0f green:51.0f / 255.0f blue:51.0f / 255.0f alpha:1.0f] CGColor],  
  42.          nil];  
  43.         [btn.layer insertSublayer:btnGradient atIndex:0];  
  44.            
  45.         // Round button corners  
  46.         CALayer *btnLayer = [btn layer];  
  47.         [btnLayer setMasksToBounds:YES];  
  48.         [btnLayer setCornerRadius:5.0f];  
  49.            
  50.         // Apply a 1 pixel, black border around Buy Button  
  51.         [btnLayer setBorderWidth:1.0f];  
  52.         [btnLayer setBorderColor:[[UIColor blackColor] CGColor]];  
  53.           
  54.     }  
  55.   
  56. }  
  57.   
  58. - (void)viewDidUnload  
  59. {  
  60.     [super viewDidUnload];  
  61.     // Release any retained subviews of the main view.  
  62. }  
  63.   
  64. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  65. {  
  66.     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);  
  67. }  
  68.   
  69. @end  

效果图片:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值