AJ学IOS(44)之网易彩票自定义图片在右边的Button_弹出view_ios6,7简单适配
AJ分享,必须精品
效果:
注意图里面了吗,其实那个效果做起来真的很简单,在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) {
// 添加将来需要显示的View
UIView *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;
// 显示内容view
self.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;
// 隐藏内容View
self.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_0
if (iOS7) { // 是IOS7
CGRect titleRect = [title boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:md context:nil];
titleW = titleRect.size.width;
}else
{
// 非IOS7
CGSize titleSize = [title sizeWithFont:self.myFont];
titleW = titleSize.width;
}
#else
// XCODE4
CGSize titleSize = [title sizeWithFont:self.myFont];
titleW = titleSize.width;
#endif
return 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);
}
@end
iOS6,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里面没有
#ifdef __IPHONE_7_0
if (iOS7) { // 是IOS7
CGRect titleRect = [title boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:md context:nil];
titleW = titleRect.size.width;
}else
{
// 非IOS7
CGSize titleSize = [title sizeWithFont:self.myFont];
titleW = titleSize.width;
}
#else
// XCODE4
CGSize titleSize = [title sizeWithFont:self.myFont];
titleW = titleSize.width;
#endif
具体如何重写的实现自定义图片在右边的button
看NYTitleButton.m(在上面)
弹出view
这个没啥好说的,就是开始定义一个view,然后设置hidden 看代码
懒加载contentView 并且在开始调用的适合(viewDidLoad)中设置隐藏。
/**懒加载,点击标题弹出的view*/
-(UIView *)contentView
{
if (_contentView == nil) {
// 添加将来需要显示的View
UIView *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;
// 显示内容view
self.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;
// 隐藏内容View
self.contentView.hidden = YES;
}
}
动画
这个搜easy
//添加动画
CATransition *ca = [CATransition animation];
ca.type = @"cube";
[self.contentView.layer addAnimation:ca forKey:nil];
AJ学IOS(44)之网易彩票自定义图片在右边的Button_弹出view_ios6,7简单适配的更多相关文章
- AJ学IOS(11)UI之图片自动轮播
AJ分享,必须精品 先看效果 代码 #import "NYViewController.h" #define kImageCount 5 @interface NYViewCont ...
- AJ学IOS(07)UI之UITextField代理事件_类似QQ登陆窗口的简单实现
AJ分享,必须精品 先看效果图: 学习代码 // // NYViewController.m // 05-UITextField事件_UIKit复习 // // Created by apple on ...
- AJ学IOS(46)之网易彩票幸运大转盘
AJ分享,必须精品 效果 实现过程: 基础UI搭建 这里主要是用了xib搭建,首先我们分析,有中间的开始按钮,背景图片,还有星座按钮,这里能用xib做的事开始按钮和背景图片. 如图: 星座按钮的搭建: ...
- AJ学IOS 之微博项目实战(2)微博主框架-自定义导航控制器NavigationController
AJ分享,必须精品 一:添加导航控制器 上一篇博客完成了对底部的TabBar的设置,这一章我们完成自定义导航控制器(NYNavigationController). 为啥要做自定义呢,因为为了更好地封 ...
- AJ学IOS(13)UI之UITableView学习(下)汽车名牌带右侧索引
AJ分享,必须精品 先看效果图 代码 ViewController #import "NYViewController.h" #import "NYCarGroup.h& ...
- AJ学IOS 之ipad开发Popover的基本使用
AJ分享,必须精品 一:效果图 二:注意 对于方法[UIPopoverController dealloc] reached while popover is still visible. 当popo ...
- AJ学IOS 之CoreLocation基本使用
猫猫分享,必须精品AJ 一:CoreLocation的基本使用 使用步骤: 首先导入头文件#import <CoreLocation/CoreLocation.h> 1.创建CoreLoc ...
- AJ学IOS(24)UI之注册案例
AJ分享,必须精品 先看效果 制作思路 在做这个的时候,首先用stroyboard画出来界面UI,这个很简单,不多说了,然后下一步就是自定义xib做键盘上面的那一栏了,需要自己做xib还有view,详 ...
- AJ学IOS(22)UI之UIApplicationDelegate和UIWindow
AJ分享,必须精品 UIApplicationDelegate 每次新建完项目,都有个带有“AppDelegate”字眼的类,它就是UIApplication的代理 NYAppDelegate默认已经 ...
随机推荐
- springcloud基础-eureka(注册中心)案例
一.新建项目,取名eureka-server pom.xml <?xml version="1.0" encoding="UTF-8"?> < ...
- gRPC (1):入门及服务端创建和调用原理
1. RPC 入门 1.1 RPC 框架原理 RPC 框架的目标就是让远程服务调用更加简单.透明,RPC 框架负责屏蔽底层的传输方式(TCP 或者 UDP).序列化方式(XML/Json/ 二进制)和 ...
- Requests库发送post请求,传入接口参数后报JSON parse error
报错信息: {"timestamp":"2020-01-08T14:42:40.894+0000","status":400,"e ...
- Linux 基础篇
本文使用系统为 :CentOS 7 1.文件操作 增: touch a.txt #增加文件 mkdir test # 增加目录 参数:-p 创建多级目录 ; 删: rm a.txt # ...
- 关于MySQL 建表的一些建议
由于在生产环境下,我们对MySQL数据库的操作通常是通过命令行进行操作,因此,建议建表的时候也手写MySQL语句(不建议用图形界面建表). 1.添加注释的格式 在编写MySQL语句时,我们通常会被要求 ...
- 概率-Knight Probability in Chessboard
2018-07-14 09:57:59 问题描述: 问题求解: 本题本质上是个挺模板的题目.本质是一个求最后每个落点的数目,用总的数目来除有所可能生成的可能性.这种计数的问题可以使用动态规划来进行解决 ...
- 动态网站项目(Dynamic Web Project)CRUD(增删改查)功能的实现(mvc(五层架构)+jdbc+servlet+tomcat7.0+jdk1.8),前端使用JSP+JSTL+EL组合
代码分享链接 https://pan.baidu.com/s/1UM0grvpttHW9idisiqa6rA 提取码:hx7c 图示 项目结构 1.SelectAllUser ...
- 写爬虫爬了3w条职位数据,看看当前招聘形势 | 开源
最近有不少程序员又开始找工作了,为了了解目前技术类各职位的数量.薪资.招聘公司.岗位职责及要求,我爬取了拉勾网北上广深4个城市的招聘数据,共3w条.职位包括:人工智能(AI).大数据.数据分析.后端( ...
- java基本数据类型和引用数据类型的调用传递的区别
(1)基本数据类型:就是进行了值的传递把一份数据拷贝了之后传递过去 (2)引用数据类型:实际上也是进行了数据拷贝然后传过去,实际上也是值传递,只不过传递过去的值和原有的值指向了同一个对象 所以在调用的 ...
- OpenCV-Python 理解特征 | 三十六
目标 在本章中,我们将尝试理解什么是特征,为什么拐角重要等等 解释 你们大多数人都会玩拼图游戏.你会得到很多小图像,需要正确组装它们以形成大的真实图像.问题是,你怎么做?将相同的理论投影到计算机程序上 ...