UIButton透明度
2015-08-21 11:34
471 查看
超级猜图游戏图片放大效果图。
实现效果:点击图像或者大图按钮,图片会以动画的方式放大到宽度等于屏幕宽度,图片周围蒙上一层深色背景,点击背景的任何地方可以还原图片,但是不能响应背景后面的Action。
背景部分是可以点击的,那么它就是UIButton。在需要的时候创建一个UIButton,大小与屏幕的frame相等,背景色为浅色,背景色透明度为0.5,然后将Button加到self.view上。
- (UIButton *)cover
{
if (_cover == nil)
{
_cover = [[UIButton alloc] initWithFrame:self.view.frame];
_cover.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.5];
[self.view addSubview:_cover];
_cover.alpha = 0.0;
[_cover addTarget:self action:@selector(bigImage:) forControlEvents:UIControlEventTouchUpInside];
}
return _cover;
}
为什么_cover.alpha = 0.0呢,两个原因:1.为了判断到底是放大还是缩小,2.如果刚开始透明度不为0.则被该Button挡住的Action无法响应鼠标事件。
- (IBAction)bigImage:(UIButton *)sender
{
if (self.cover.alpha == 0)
{
//将self.btnImageIcon放在所以subview的最前面
[self.view bringSubviewToFront:self.btnImageIcon];
CGFloat iconX = 0;
CGFloat iconW = self.view.frame.size.width;
CGFloat iconH = iconW;
CGFloat iconY = (self.view.frame.size.height - iconH) * 0.5;
//执行动画,放大imageIcon,并且设置透明度。
[UIView animateWithDuration:1.0 animations:^{
self.btnImageIcon.frame = CGRectMake(iconX, iconY, iconW, iconH);
self.cover.alpha = 1.0;
}];
}
else
{
[UIView animateWithDuration:1.0 animations:^{
self.btnImageIcon.frame = CGRectMake(75, 95, 225, 225);
//将UIButton的透明度设置为0,就相当于设置setHidden
= YES
self.cover.alpha = 0.0;
}];
}
}
由此可以看出,当self.cover.alpha = 0.0时,相当于self.cover.hidden
= YES,不会影响self.cover后面的Action事件响应。
还有就是,- (UIButton *)cover,使用的是懒加载方式,就是在第一次使用的时候才会去执行此代码。懒加载简单说就是重写@property
(nonatomic,strong)UIButton
*cover属性的get方法。
;
相关文章推荐
- 蓝牙UUID及其128位换算
- Circular Queue--循环队列
- hdu 5400 Arithmetic Sequence
- HDU 4746 Mophues (莫比乌斯反演应用)
- 代码实现UIPickerView
- 第十四篇 continue VS. break
- CoreBluetooth使用 (服务端中心设备)
- Xcode强大的多视图立体分层显示View UI Herarchy
- Hibernate SQL优化技巧dynamic-insert="true" dynamic-update="true"
- Jsp重定向(response.sendRedirect())和转发(request.getRequestDispatcher().forward(request,r)的区别
- Errors occurred during the build. Errors running builder 'DeploymentBuilder' on project '项目名'
- 在Android Studio中进行单元测试和UI测试
- Uiautomator Viewer中文显示乱码问题
- The requested URL /get_data.xml was not found on this server.
- spoj 375 QTREE - Query on a tree 树链剖分 LCT 动态树
- UIDeviceOrientation和UIInterfaceOrientation中left、right的含义
- Pop Sequence (25)--栈模拟经典
- The type java.util.Map$Entry cannot be resolved. It is indirectly referenced from required .class
- [LeetCode#52]N-Queens II
- Android开源资料大集合_架构&UI