猫猫学iOS(四十四)之网易彩票自定义图片在右边的Button_弹出view_ios6,7简单适配...
貓貓分享,必須精品
原創文章,歡迎轉載。轉載請注明:翟乃玉的博客
地址:http://blog.csdn.net/u013357243?viewmode=contents
效果:
注意圖里面了嗎,其實那個效果做起來真的很簡單,在iOS中蘋果給我們封裝的很好,關鍵是那個按鈕
系統的按鈕的圖片是在左邊的,這里我們需要把他調整到右邊,然后呢需要我們自己做一下操作。
代碼:
話不多說,先把所有代碼放上來。能看懂就不用看別的了。(這么詳細的注釋,看不懂才怪。。)
彈出view:NYBuyController.m
// // NYBuyController.m // 彩票lottery // // Created by apple on 15-5-10. // Copyright (c) 2015年 znycat. All rights reserved. //#import "NYBuyController.h" #import "NYTitleButton.h"@interface NYBuyController ()- (IBAction)titleBtnOnClick:(NYTitleButton *)titleBtn;// 定義變量記錄當前按鈕的狀態 @property (nonatomic, assign, getter = isOpen) BOOL open;@property (nonatomic, weak) UIView *contentView;@end@implementation NYBuyController/**懶加載,點擊標題彈出的view*/ -(UIView *)contentView {if (_contentView == nil) {// 添加將來需要顯示的ViewUIView *contentView = [[UIView alloc] init];contentView.backgroundColor = [UIColor greenColor];contentView.frame = CGRectMake(0, 64, 320, 200);[self.view addSubview:contentView];_contentView = contentView;// 隱藏該View}return _contentView; }- (void)viewDidLoad {[super viewDidLoad];self.contentView.hidden = YES; }- (IBAction)titleBtnOnClick:(NYTitleButton *)titleBtn {if (!self.isOpen) {// 沒有打開[UIView animateWithDuration:1.0 animations:^{// 1.旋轉按鈕上的尖尖titleBtn.imageView.transform = CGAffineTransformMakeRotation(M_PI);}];// 改變當前按鈕的狀態CATransition *ca = [CATransition animation];ca.type = @"cube";[self.contentView.layer addAnimation:ca forKey:nil];self.open = YES;// 顯示內容viewself.contentView.hidden = NO;}else // 已經打開{[UIView animateWithDuration:1.0 animations:^{// 1.旋轉按鈕上的尖尖titleBtn.imageView.transform = CGAffineTransformIdentity;}];// 改變當前按鈕的狀態//添加動畫CATransition *ca = [CATransition animation];ca.type = @"cube";[self.contentView.layer addAnimation:ca forKey:nil];self.open = NO;// 隱藏內容Viewself.contentView.hidden = YES;}} @end:自定義圖片在右邊的Button NYTitleButton.m
// // NYTitleButton.m // 彩票lottery // // Created by apple on 15-5-10. // Copyright (c) 2015年 znycat. All rights reserved. //#import "NYTitleButton.h"@interface NYTitleButton ()@property (nonatomic, strong) UIFont *myFont;@end@implementation NYTitleButton-(id)initWithCoder:(NSCoder *)aDecoder {if (self = [super initWithCoder:aDecoder]) {[self setup];}return self; }-(id)initWithFrame:(CGRect)frame {if (self = [super initWithFrame:frame]) {[self setup];}return self; }-(void)setup {// 記錄按鈕標題的字體self.myFont = [UIFont systemFontOfSize:16];// 設置標題的字體self.titleLabel.font = self.myFont;// 設置按鈕的圖片顯示的內容默認為劇中(為了不拉伸)self.imageView.contentMode = UIViewContentModeCenter; }// 用于返回按鈕上標題的位置, 傳入按鈕的rect - (CGRect)titleRectForContentRect:(CGRect)contentRect {CGFloat titleX = 0;CGFloat titleY = 0;CGFloat titleH = contentRect.size.height;// 獲取當前按鈕上的文字// [self titleForState:UIControlStateNormal];NSString *title = self.currentTitle;CGSize maxSize = CGSizeMake(MAXFLOAT, MAXFLOAT);NSMutableDictionary *md = [NSMutableDictionary dictionary];md[NSFontAttributeName] = self.myFont;// 計算文字的范圍CGFloat titleW = 0;// 判斷是否是xcode5 , 如果是就編譯一下代碼, 如果不是就不編譯 #ifdef __IPHONE_7_0if (iOS7) { // 是IOS7CGRect titleRect = [title boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:md context:nil];titleW = titleRect.size.width;}else{// 非IOS7CGSize titleSize = [title sizeWithFont:self.myFont];titleW = titleSize.width;} #else// XCODE4CGSize titleSize = [title sizeWithFont:self.myFont];titleW = titleSize.width; #endifreturn CGRectMake(titleX, titleY, titleW, titleH); } - (CGRect)imageRectForContentRect:(CGRect)contentRect {CGFloat imageY = 0;CGFloat imageH = contentRect.size.height;CGFloat imageW = 16;// 圖片的X = 按鈕的寬度 - 圖片寬度CGFloat imageX = contentRect.size.width - imageW;return CGRectMake(imageX, imageY, imageW, imageH); }@endiOS6,7簡單適配 文件: (Prefix.pch)
#define iOS7 ([[UIDevice currentDevice].systemVersion doubleValue] >= 7.0)具體實現
把按鈕圖片放右邊(自定義圖片在右邊的按鈕)
要想實現自定義按鈕位置 主要是重寫下面兩個方法
- (CGRect)titleRectForContentRect:(CGRect)contentRect; - (CGRect)imageRectForContentRect:(CGRect)contentRect;這里上面代碼中寫的很清楚了,不多說了就,需要注意的是,我們在算title的長度的時候出現了一些狀況,那就是iOS6里面沒有這個方法,以及xcode4.5版本會編譯出錯的問題,在這里做了iOS6,7的適配以及編譯器的適配:
(iOS7)這個是宏,在pch文件里面有定義
#ifdef __IPHONE_7_0 是Availability.h里的 xcode4.5里面沒有
具體如何重寫的實現自定義圖片在右邊的button
看NYTitleButton.m(在上面)
彈出view
這個沒啥好說的,就是開始定義一個view,然后設置hidden 看代碼
懶加載contentView 并且在開始調用的適合(viewDidLoad)中設置隱藏。
/**懶加載,點擊標題彈出的view*/ -(UIView *)contentView {if (_contentView == nil) {// 添加將來需要顯示的ViewUIView *contentView = [[UIView alloc] init];contentView.backgroundColor = [UIColor greenColor];contentView.frame = CGRectMake(0, 64, 320, 200);[self.view addSubview:contentView];_contentView = contentView;// 隱藏該View}return _contentView; }- (void)viewDidLoad {[super viewDidLoad];self.contentView.hidden = YES; }在點擊按鈕時候設置隱藏為no或yes,這里加了兩個動畫而已
- (IBAction)titleBtnOnClick:(NYTitleButton *)titleBtn {if (!self.isOpen) {// 沒有打開[UIView animateWithDuration:1.0 animations:^{// 1.旋轉按鈕上的尖尖titleBtn.imageView.transform = CGAffineTransformMakeRotation(M_PI);}];// 改變當前按鈕的狀態CATransition *ca = [CATransition animation];ca.type = @"cube";[self.contentView.layer addAnimation:ca forKey:nil];self.open = YES;// 顯示內容viewself.contentView.hidden = NO;}else // 已經打開{[UIView animateWithDuration:1.0 animations:^{// 1.旋轉按鈕上的尖尖titleBtn.imageView.transform = CGAffineTransformIdentity;}];// 改變當前按鈕的狀態//添加動畫CATransition *ca = [CATransition animation];ca.type = @"cube";[self.contentView.layer addAnimation:ca forKey:nil];self.open = NO;// 隱藏內容Viewself.contentView.hidden = YES;}}動畫
這個搜easy,在前面有http://blog.csdn.net/u013357243/article/details/45583423
//添加動畫CATransition *ca = [CATransition animation];ca.type = @"cube";[self.contentView.layer addAnimation:ca forKey:nil];轉載于:https://www.cnblogs.com/znycat/p/4521003.html
總結
以上是生活随笔為你收集整理的猫猫学iOS(四十四)之网易彩票自定义图片在右边的Button_弹出view_ios6,7简单适配...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电饼铛品牌(电的发展历史)
- 下一篇: Keychron Q12 客制化机械键盘