呼吸效果:传说中的“呼吸__灯__效果 ”,美观又起到了提示效果
gif图 只有单一的“呼”(或者“吸”)效果,没有对应的“吸”(或许“呼”)效果!见谅~~
亮度到达最高值,瞬间变为最低值(个人觉得会有些突兀~ ),又慢慢到达最高值。
这个倒是有“呼”有“吸”,但是棒子。。却不是很好看。。。
核心代码:一个方法的执行进入无限循环( 呼吸(明暗变化)效果 )!!
-(void)startWithLight { // 明效果 开始
[UIView animateWithDuration:1.8f animations:^{
_breatheV.alpha = 0.35f;
//设置UIView设置alpha 或者 设置其layer层的opacity时,其Subview的透明度也会跟着变。
//_breatheV.backgroundColor = [_breatheV.backgroundColor colorWithAlphaComponent:0.35f]; // 不影响子视图的设置
} completion:^(BOOL finished) {
//结束后 开启“-(void)startWithDark { }”方法
[self startWithDark];
}];
}
-(void)startWithDark { // 暗效果 开始
[UIView animateWithDuration:1.8f animations:^{
_breatheV.alpha = 0.1f;
//_breatheV.backgroundColor = [_breatheV.backgroundColor colorWithAlphaComponent:0.1f];
//不影响子视图的设置
} completion:^(BOOL finished) {
//结束后 开启“-(void)startWithLight { }”方法
[self startWithLight];
}];
}
复制代码
设置UIView设置alpha 或者 设置其layer层的opacity时,其Subview的透明度也会跟着变 。
// 宏定义:屏宽
#define kScreenWidth \
([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)] ? [UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale : [UIScreen mainScreen].bounds.size.width)
@interface ViewController () {
UIView * _breatheV; // 定义 全局视图变量
}
@end
复制代码
在“- (void)viewDidLoad { }”方法里面:
_breatheV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth/2.f, 50)];
_breatheV.center = CGPointMake(kScreenWidth/2.f, 20 + 50/2.f);
_breatheV.backgroundColor = [UIColor purpleColor];
_breatheV.alpha = 0.35f; // 开始的状态 半透明效果
[self.view addSubview:_breatheV];
// 开启 呼吸效果(无限动画)
[self startWithDark];
复制代码
到这里其实基础好的伙伴 已经不用看后面的内容!
定制一个呼吸效果的按钮
1.选择“New File”,新建一个文件
2.选择选择“Cocoa Touch Clas”
3.选择继承自UIView
#import <UIKit/UIKit.h>
@interface GoyoholButton : UIView
-(instancetype)initWithFrame:(CGRect)frame WithColor:(UIColor *)color WithImageName:(NSString *)imgNameStr;
@end
复制代码
#import "GoyoholButton.h"
@interface GoyoholButton ()
@property (nonatomic , retain ) UIView * backgroundView;//背景视图
@property (nonatomic , retain ) UIImageView * imageView;//图片视图
@end
@implementation GoyoholButton
//初始化
-(instancetype)initWithFrame:(CGRect)frame WithColor:(UIColor *)color WithImageName:(NSString *)imgNameStr {
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor whiteColor];
//初始化背景视图
_backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
_backgroundView.backgroundColor = color;
_backgroundView.userInteractionEnabled = NO; // 可有可无
[self addSubview:_backgroundView];
//初始化图片背景视图
_imageView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:imgNameStr] imageWithRenderingMode:UIImageRenderingModeAutomatic]];//取图片 渲染
//_imageView.tintColor = [UIColor whiteColor];//图片渲染颜色
_imageView.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame) );
[_backgroundView addSubview:_imageView]; // 添加到背景视图上
// 按钮 的 添加
UIButton * actionBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
actionBtn.backgroundColor = [UIColor clearColor];
[actionBtn addTarget:self action:@selector(actionResponse) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:actionBtn];
//开启呼吸动画
[self HighlightAnimation];
}
return self;
}
复制代码
#pragma mark ---BreathingAnimation 呼吸动画
- (void)HighlightAnimation{
__block typeof(self) weakSelf = self;
[UIView animateWithDuration:1.5f animations:^{
weakSelf.backgroundView.alpha = 0.3f;
} completion:^(BOOL finished) {
[weakSelf DarkAnimation];
}];
}
- (void)DarkAnimation{
__block typeof(self) weakSelf = self;
[UIView animateWithDuration:1.5f animations:^{
weakSelf.backgroundView.alpha = 0.8f;
} completion:^(BOOL finished) {
[weakSelf HighlightAnimation];
}];
}
复制代码
ViewController
#import “GoyoholButton.h” //导入按钮类
的“- (void)viewDidLoad { }”里面:
GoyoholButton * goyoholBtn = [[GoyoholButton alloc] initWithFrame:CGRectMake(10, 100, 100, 150) WithColor:[UIColor blueColor] WithImageName:@"cookie.jpg"];
[self.view addSubview:goyoholBtn];
复制代码
效果:整个视图self.backgroundView的“alpha通道”值 改变
按钮点击事件响应 效果:
-
1.仅仅图片 呼吸效果 (self.backgroundView的背景色 为 “蓝色”)
#pragma mark ---BreathingAnimation 呼吸动画 - (void)HighlightAnimation{ __block typeof(self) weakSelf = self; [UIView animateWithDuration:1.5f animations:^{ weakSelf.imageView.alpha = 0.3f; } completion:^(BOOL finished) { [weakSelf DarkAnimation]; }]; } - (void)DarkAnimation{ __block typeof(self) weakSelf = self; [UIView animateWithDuration:1.5f animations:^{ weakSelf.imageView.alpha = 0.8f; } completion:^(BOOL finished) { [weakSelf HighlightAnimation]; }]; } 复制代码
效果:只有图片视图(self.imageView)的“alpha通道”值 改变
-
2.backgroundView进行不影响子视图的变换(self.backgroundView的背景色 为 “蓝色”) 呼吸效果
#pragma mark ---BreathingAnimation 呼吸动画 - (void)HighlightAnimation{ __block typeof(self) weakSelf = self; [UIView animateWithDuration:1.5f animations:^{ // backgroundView⭐️不影响子视图的变换 weakSelf.backgroundView.backgroundColor = [weakSelf.backgroundView.backgroundColor colorWithAlphaComponent:0.2f]; weakSelf.imageView.alpha = 0.3f; } completion:^(BOOL finished) { [weakSelf DarkAnimation]; }]; } - (void)DarkAnimation{ __block typeof(self) weakSelf = self; [UIView animateWithDuration:1.5f animations:^{ // backgroundView不影响子视图的变换 weakSelf.backgroundView.backgroundColor = [weakSelf.backgroundView.backgroundColor colorWithAlphaComponent:0.8f]; weakSelf.imageView.alpha = 0.8f; } completion:^(BOOL finished) { [weakSelf HighlightAnimation]; }]; } 复制代码
效果:同时执行了“alpha通道”值 改变(由于backgroundView不影响子视图imageView的变换,故 两个呼吸效果 一起展示)。
查看一下不影响子视图的效果
// 将 图片缩小一半
_imageView.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame)/2.f );
#pragma mark ---BreathingAnimation 呼吸动画
- (void)HighlightAnimation{
__block typeof(self) weakSelf = self;
[UIView animateWithDuration:1.5f animations:^{
weakSelf.backgroundView.backgroundColor = [weakSelf.backgroundView.backgroundColor colorWithAlphaComponent:0.2f];
} completion:^(BOOL finished) {
[weakSelf DarkAnimation];
}];
}
- (void)DarkAnimation{
__block typeof(self) weakSelf = self;
[UIView animateWithDuration:1.5f animations:^{
weakSelf.backgroundView.backgroundColor = [weakSelf.backgroundView.backgroundColor colorWithAlphaComponent:0.8f];
} completion:^(BOOL finished) {
[weakSelf HighlightAnimation];
}];
}
复制代码
效果:自身的透明度改变,却不影响 子视图的透明度!
若需要在Viewcontroller里面实现 点击响应:
//创建两个属性
@property (nonatomic,strong) id target; //存响应消息的对象
@property (nonatomic,assign) SEL action;//存响应消息
复制代码
在“-(void)actionResponse { }”里面:
//让对象响应消息,传递按钮点击事件
if ([self.target respondsToSelector:self.action]) {
[self.target performSelector:self.action withObject:self];
} else {
NSLog(@"没有实现方法");
}
复制代码
再存储其方法:
#pragma mark - 添加事件响应
-(void)addTarget:(id)target action:(SEL)action
{ //传过来的消息⭐️存起来,再 在合适的时候 执行 [self.target self.action]
self.target = target;
self.action = action;
}
复制代码
-(void)addTarget:(id)target action:(SEL)action;
复制代码
便可以在Viewcontroller的“- (void)viewDidLoad { }”里面使用了:
[goyoholBtn addTarget:self action:@selector(actionClick)];
-(void)actionClick {
NSLog(@"dafdadfadfadfa");
}
复制代码
效果:
到此呼吸效果按钮就说完了!只是讲了大概的定制方式!突然发现自己以前好啰嗦(最近看了以前带我入门的大神 写的文章,感觉好惭愧~~?),现在想简洁明了一点~
Tips:(加个餐)如果觉得警告(⚠️)看上去 很不爽, 如下消除:
产生了“performSelector may cause a leak because its selector is unknown”警告
用简单、粗暴的方式 解决“警告显示问题”
#pragma clang diagnostic push
#pragma clang diagnostic ignored"-Warc-performSelector-leaks" //根据警告(类型)提示 ,选择 字串
//这里是会报警告的代码
#pragma clang diagnostic pop
复制代码
把需要的字串填入上边的ignored的后边 (简单粗暴:f式 ?)
点击进入网站,查看要忽略各种警告 对应的字串:Which Clang Warning Is Generating This Message?
网站的截图: