您的位置:首页 > 产品设计 > UI/UE

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方法。

;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: