[iOS UI进阶 - 2.1] 彩票Demo v1.1
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 2 // Override point for customization after application launch. 3 4 // 设置状态栏样式为白色 5 application.statusBarHidden = NO; 6 application.statusBarStyle = UIStatusBarStyleLightContent; 7 8 return YES; 9 }
1 // 2 // TitleExtendButton.m 3 // HelloLottery 4 // 5 // Created by hellovoidworld on 15/1/3. 6 // Copyright (c) 2015年 hellovoidworld. All rights reserved. 7 // 8 9 #import "TitleExtendButton.h" 10 11 @interface TitleExtendButton() 12 13 @property(nonatomic, strong) UIFont *titleFont; 14 15 @end 16 17 @implementation TitleExtendButton 18 19 /** 从文件加载对象就会调用此方法,例如xib和storybard */ 20 - (id)initWithCoder:(NSCoder *)aDecoder { 21 NSLog(@"从文件加载TitleButton"); 22 if (self = [super initWithCoder:aDecoder]) { 23 [self initializeButton]; 24 } 25 26 return self; 27 } 28 29 /** 从代码中加载对象就会调用此方法 */ 30 - (instancetype)initWithFrame:(CGRect)frame { 31 NSLog(@"从代码加载TitleButton"); 32 if (self = [super initWithFrame:frame]) { 33 [self initializeButton]; 34 } 35 36 return self; 37 } 38 39 - (void) initializeButton { 40 // 设置font 41 self.titleFont = [UIFont systemFontOfSize:14]; // 暂时先自定义font 42 self.titleLabel.font = self.titleFont; 43 44 // 图标居中 45 [self.imageView setContentMode:UIViewContentModeCenter]; 46 } 47 48 49 /** 返回title的frame */ 50 - (CGRect)titleRectForContentRect:(CGRect)contentRect { 51 CGFloat titleX = 0; 52 CGFloat titleY = 0; 53 54 NSDictionary *attr = @{NSFontAttributeName : self.titleFont}; 55 CGFloat titleWidth; 56 57 // 只有 iOS7 版本以上才能运行以下代码 58 if (iOS7) { 59 // 只有 Xcode5 或以上版本才能识别这段代码 60 #ifdef __IPHONE_7_0 61 titleWidth = [self.currentTitle boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attr context:nil].size.width; 62 #else 63 titleWidth = [self.currentTitle sizeWithFont:self.titleFont].width; 64 #endif 65 } else { // 否则使用旧的API 66 titleWidth = [self.currentTitle sizeWithFont:self.titleFont].width; 67 } 68 69 CGFloat titleHeight = contentRect.size.height; 70 71 return CGRectMake(titleX, titleY, titleWidth, titleHeight); 72 } 73 74 /** 返回image的frame */ 75 - (CGRect)imageRectForContentRect:(CGRect)contentRect { 76 CGFloat imageWidth = 30; 77 CGFloat imageHeight = contentRect.size.height; 78 CGFloat imageX = contentRect.size.width - imageWidth; 79 CGFloat imageY = 0; 80 return CGRectMake(imageX, imageY, imageWidth, imageHeight); 81 } 82 83 @end
1 // 2 // HVWBuyViewController.m 3 // HelloLottery 4 // 5 // Created by hellovoidworld on 15/1/3. 6 // Copyright (c) 2015年 hellovoidworld. All rights reserved. 7 // 8 9 #import "HVWBuyViewController.h" 10 #import "TitleExtendButton.h" 11 12 @interface HVWBuyViewController () 13 14 @property(nonatomic, weak) UIView *popupView; 15 16 /** 标题点击事件 */ 17 - (IBAction)titleClicked:(TitleExtendButton *)sender; 18 19 @end 20 21 @implementation HVWBuyViewController 22 23 - (void)viewDidLoad { 24 [super viewDidLoad]; 25 // Do any additional setup after loading the view. 26 27 } 28 29 - (void)didReceiveMemoryWarning { 30 [super didReceiveMemoryWarning]; 31 // Dispose of any resources that can be recreated. 32 } 33 34 /** 延迟初始化弹出view 35 * (发现放在viewDidLoad的时候,在点击按钮调用的时候pupupView的frame没有被初始化) 36 */ 37 - (UIView *)popupView { 38 if (_popupView == nil) { 39 // 初始化弹出view 40 UIView *popupView = [[UIView alloc] init]; 41 CGFloat popupViewX = 0; 42 CGFloat popupViewY = [UIApplication sharedApplication].statusBarFrame.size.height + self.navigationController.navigationBar.frame.size.height; 43 CGFloat popupViewWidth = self.navigationController.navigationBar.frame.size.width; 44 CGFloat popupViewHeight = self.view.frame.size.height - popupViewY - self.tabBarController.tabBar.frame.size.height; 45 popupView.frame = CGRectMake(popupViewX, popupViewY, popupViewWidth, popupViewHeight); 46 popupView.backgroundColor = [UIColor grayColor]; 47 48 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 200, 100)]; 49 label.text = @"这是全部彩种de弹出内容"; 50 [popupView addSubview:label]; 51 52 self.popupView = popupView; 53 NSLog(@"%@", NSStringFromCGRect(self.popupView.frame)); 54 } 55 56 return _popupView; 57 } 58 59 /** 标题点击事件 60 * 转换箭头方向 61 * 伸缩内容 62 */ 63 - (IBAction)titleClicked:(TitleExtendButton *)sender { 64 [UIView animateWithDuration:0.5 animations:^{ 65 // 按钮旋转 66 sender.imageView.transform = CGAffineTransformRotate(sender.imageView.transform, M_PI); 67 }]; 68 69 // 弹出view 70 if (![self.popupView isDescendantOfView:self.view]) { 71 [self.view addSubview:self.popupView]; 72 } else { 73 [self.popupView removeFromSuperview]; 74 } 75 } 76 @end
1 #define __IPHONE_2_0 20000 2 #define __IPHONE_2_1 20100 3 #define __IPHONE_2_2 20200 4 #define __IPHONE_3_0 30000 5 #define __IPHONE_3_1 30100 6 #define __IPHONE_3_2 30200 7 #define __IPHONE_4_0 40000 8 #define __IPHONE_4_1 40100 9 #define __IPHONE_4_2 40200 10 #define __IPHONE_4_3 40300 11 #define __IPHONE_5_0 50000 12 #define __IPHONE_5_1 50100 13 #define __IPHONE_6_0 60000 14 #define __IPHONE_6_1 60100 15 #define __IPHONE_7_0 70000 16 #define __IPHONE_7_1 70100 17 #define __IPHONE_8_0 80000 18 #define __IPHONE_8_1 80100
1 // 2 // UIImage+Extend.m 3 // HelloLottery 4 // 5 // Created by hellovoidworld on 15/1/3. 6 // Copyright (c) 2015年 hellovoidworld. All rights reserved. 7 // 8 9 #import "UIImage+Extend.h" 10 11 @implementation UIImage(Extend) 12 13 /** 返回一个中心扩展拉伸的图片 */ 14 + (UIImage *) resizableImage:(NSString *) imageName { 15 UIImage *image = [UIImage imageNamed:imageName]; 16 17 // 这个参数决定了左边的保护区域,右边的保护区域为左边+1开始到末端 18 CGFloat width = image.size.width * 0.5; 19 20 // 原理同左右保护区域 21 CGFloat height = image.size.height * 0.5; 22 23 // 也就是,取中间1x1的区域作为扩展区域 24 return [image stretchableImageWithLeftCapWidth:width topCapHeight:height]; 25 } 26 27 @end
1 // 2 // HVWLoginViewController.m 3 // HelloLottery 4 // 5 // Created by hellovoidworld on 15/1/3. 6 // Copyright (c) 2015年 hellovoidworld. All rights reserved. 7 // 8 9 #import "HVWLoginViewController.h" 10 #import "UIImage+Extend.h" 11 12 @interface HVWLoginViewController () 13 14 /** 登陆按钮 */ 15 @property (weak, nonatomic) IBOutlet UIButton *loginButton; 16 17 @end 18 19 @implementation HVWLoginViewController 20 21 - (void)viewDidLoad { 22 [super viewDidLoad]; 23 // Do any additional setup after loading the view. 24 25 UIImage *normal = [UIImage resizableImage:@"RedButton"]; 26 UIImage *highlighted = [UIImage resizableImage:@"RedButtonPressed"]; 27 28 [self.loginButton setBackgroundImage:normal forState:UIControlStateNormal]; 29 [self.loginButton setBackgroundImage:highlighted forState:UIControlStateHighlighted]; 30 } 31 32 - (void)didReceiveMemoryWarning { 33 [super didReceiveMemoryWarning]; 34 // Dispose of any resources that can be recreated. 35 } 36 37 38 @end
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。