button控制视图的隐藏与显示
2016-07-21 20:35
447 查看
在同一位置设置按钮的切换,同时伴随着对应页面的跳转,在平时经常会遇到。这里重点实现按钮的隐藏和显示效果,特意理一下思路。欢迎交流分享。
实现按钮的切换效果。这里按钮设置在导航栏的左侧:
代码:
- (void) _createNavigationRightItem {
//父视图
UIView *customView = [[UIView
alloc] initWithFrame:CGRectMake(0, 0, 49, 32)];
customView.backgroundColor = [UIColor
clearColor];
customView.tag = 1000;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem
alloc] initWithCustomView:customView];
//两个子视图
UIButton *postButton = [UIButton
buttonWithType:UIButtonTypeCustom];
[postButton setImage:[UIImage
imageNamed:@"poster_home"]
forState:UIControlStateNormal];
[postButton setBackgroundImage:[UIImage
imageNamed:@"exchange_bg_home@2x"]
forState:UIControlStateNormal];
postButton.frame = customView.bounds;
postButton.tag = 1001;
postButton.hidden =
YES;
[postButton addTarget:self
action:@selector(rightAction:)
forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:postButton];
UIButton *listButton = [UIButton
buttonWithType:UIButtonTypeCustom];
[listButton setImage:[UIImage
imageNamed:@"list_home"]
forState:UIControlStateNormal];
[listButton setBackgroundImage:[UIImage
imageNamed:@"exchange_bg_home@2x"]
forState:UIControlStateNormal];
listButton.frame = customView.bounds;
listButton.tag = 1002;
listButton.hidden =
NO;
[listButton addTarget:self
action:@selector(rightAction:)
forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:listButton];
}
设置好一个父视图,将下一步要添加的按钮放上去,需要两个按钮来实现它的切换效果。这里一共创建了三个视图,分别设置好了背景和图片,而且设置了tag值。按钮的点击事件响应在同一个方法里面。
接下来我们要实现它的点击事件:
- (void)rightAction:(UIButton *) btn {
//获取对应视图tag值
UIButton *btn1 = [self.navigationController.navigationBar
viewWithTag:1001];
UIButton *btn2 = [self.navigationController.navigationBar
viewWithTag:1002];
UIView *view = [self.navigationController.navigationBar
viewWithTag:1000];
UIViewAnimationOptions option;
BOOL flip = btn1.hidden;
//button的切换效果设置
if (flip ) {
//翻转效果
option = UIViewAnimationOptionTransitionFlipFromLeft;
} else {
option = UIViewAnimationOptionTransitionFlipFromRight;
}
//***button的隐藏和显示
[UIView
transitionWithView:view duration:0.35
options:option animations:^{
btn1.hidden = !btn1.hidden;
btn2.hidden = !btn2.hidden;
} completion:NULL];
//页面的隐藏和显示(两个视图页面可以自己创建,这里就不显示了)
[UITableView
transitionWithView:listView
duration:0.35 options:option
animations:^{
listView.hidden = !listView.hidden;
postView.hidden = !postView.hidden;
} completion:NULL];
}
需要注意的是一定要记得调用方法,对两个按钮的切换和隐藏一定要思路清晰。
实现按钮的切换效果。这里按钮设置在导航栏的左侧:
代码:
- (void) _createNavigationRightItem {
//父视图
UIView *customView = [[UIView
alloc] initWithFrame:CGRectMake(0, 0, 49, 32)];
customView.backgroundColor = [UIColor
clearColor];
customView.tag = 1000;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem
alloc] initWithCustomView:customView];
//两个子视图
UIButton *postButton = [UIButton
buttonWithType:UIButtonTypeCustom];
[postButton setImage:[UIImage
imageNamed:@"poster_home"]
forState:UIControlStateNormal];
[postButton setBackgroundImage:[UIImage
imageNamed:@"exchange_bg_home@2x"]
forState:UIControlStateNormal];
postButton.frame = customView.bounds;
postButton.tag = 1001;
postButton.hidden =
YES;
[postButton addTarget:self
action:@selector(rightAction:)
forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:postButton];
UIButton *listButton = [UIButton
buttonWithType:UIButtonTypeCustom];
[listButton setImage:[UIImage
imageNamed:@"list_home"]
forState:UIControlStateNormal];
[listButton setBackgroundImage:[UIImage
imageNamed:@"exchange_bg_home@2x"]
forState:UIControlStateNormal];
listButton.frame = customView.bounds;
listButton.tag = 1002;
listButton.hidden =
NO;
[listButton addTarget:self
action:@selector(rightAction:)
forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:listButton];
}
设置好一个父视图,将下一步要添加的按钮放上去,需要两个按钮来实现它的切换效果。这里一共创建了三个视图,分别设置好了背景和图片,而且设置了tag值。按钮的点击事件响应在同一个方法里面。
接下来我们要实现它的点击事件:
- (void)rightAction:(UIButton *) btn {
//获取对应视图tag值
UIButton *btn1 = [self.navigationController.navigationBar
viewWithTag:1001];
UIButton *btn2 = [self.navigationController.navigationBar
viewWithTag:1002];
UIView *view = [self.navigationController.navigationBar
viewWithTag:1000];
UIViewAnimationOptions option;
BOOL flip = btn1.hidden;
//button的切换效果设置
if (flip ) {
//翻转效果
option = UIViewAnimationOptionTransitionFlipFromLeft;
} else {
option = UIViewAnimationOptionTransitionFlipFromRight;
}
//***button的隐藏和显示
[UIView
transitionWithView:view duration:0.35
options:option animations:^{
btn1.hidden = !btn1.hidden;
btn2.hidden = !btn2.hidden;
} completion:NULL];
//页面的隐藏和显示(两个视图页面可以自己创建,这里就不显示了)
[UITableView
transitionWithView:listView
duration:0.35 options:option
animations:^{
listView.hidden = !listView.hidden;
postView.hidden = !postView.hidden;
} completion:NULL];
}
需要注意的是一定要记得调用方法,对两个按钮的切换和隐藏一定要思路清晰。
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用开发中AFNetworking库的常用HTTP操作方法小结
- iOS应用中UISearchDisplayController搜索效果的用法
- iOS App开发中的UISegmentedControl分段组件用法总结
- IOS开发环境windows化攻略
- iOS应用中UITableView左滑自定义选项及批量删除的实现
- iOS中UIAlertView警告框组件的使用教程
- 浅析iOS应用开发中线程间的通信与线程安全问题
- iOS中的UIKeyboard键盘视图使用方法小结
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法