借鉴相关资料,整理了一个很有意思的button动画效果,iOS自定义UIbutton点击动画特效
先看一下效果图:
下面贴上代码:
VIEwController:
#import <UIKit/UIKit.h>@interface VIEwController : UIVIEwController@end#import "VIEwController.h"#import "HWbutton.h"#define mainW [UIScreen mainScreen].bounds.size.wIDth#define mainH [UIScreen mainScreen].bounds.size.height@interface VIEwController ()@end@implementation VIEwController- (voID)vIEwDIDLoad { [super vIEwDIDLoad]; self.vIEw.backgroundcolor = [UIcolor blackcolor]; //创建控件 [self creatbutton];}- (voID)creatbutton{ HWbutton *button = [[HWbutton alloc] initWithFrame:CGRectMake(mainW * 0.5 - 60,mainH - 100,120,72) maxleft:100 maxRight:100 maxHeight:300]; [button setimage:[UIImage imagenamed:@"button"] forState:UIControlStatenormal]; button.images = @[[UIImage imagenamed:@"Circle 1"],[UIImage imagenamed:@"Circle 2"],[UIImage imagenamed:@"Circle 3"],[UIImage imagenamed:@"Hero"]]; button.duration = 10; [button addTarget:self action:@selector(buttonOnClick:) forControlEvents:UIControlEventtouchUpInsIDe]; [self.vIEw addSubvIEw:button];}- (voID)buttonOnClick:(HWbutton *)btn{ [btn generateBubbleInRandom];}@end
HWbutton:
#import <UIKit/UIKit.h>@interface HWbutton : UIbutton@property (nonatomic,assign) CGfloat maxleft;@property (nonatomic,assign) CGfloat maxRight;@property (nonatomic,assign) CGfloat maxHeight;@property (nonatomic,assign) CGfloat duration;@property (nonatomic,strong) NSArray *images;- (instancetype)initWithFrame:(CGRect)frame maxleft:(CGfloat)maxleft maxRight:(CGfloat)maxRight maxHeight:(CGfloat)maxHeight;- (voID)generateBubbleWithImage:(UIImage *)image;- (voID)generateBubbleInRandom;@end#import "HWbutton.h"@implementation HWbutton{ CGPoint _startPoint; CGfloat _maxWIDth; NSMutableSet *_recyclePool; NSMutableArray *_array;}- (instancetype)initWithFrame:(CGRect)frame maxleft:(CGfloat)maxleft maxRight:(CGfloat)maxRight maxHeight:(CGfloat)maxHeight{ self = [super initWithFrame:frame]; if (self) { _maxHeight = maxHeight; _maxleft = maxleft; _maxRight = maxRight; [self initData]; } return self;}- (ID)initWithCoder:(NSCoder *)aDecoder{ self = [super initWithCoder:aDecoder]; if (self) { [self initData]; } return self;}- (voID)initData{ _array = @[].mutablecopy; _recyclePool = [NSMutableSet set];}- (voID)generateBubbleInRandom{ CALayer *layer; if (_recyclePool.count > 0) { layer = [_recyclePool anyObject]; [_recyclePool removeObject:layer]; }else { UIImage *image = self.images[arc4random() % self.images.count]; layer = [self createLayerWithImage:image]; } [self.layer addSublayer:layer]; [self generateBubbleWithCAlayer:layer];}- (voID)generateBubbleWithImage:(UIImage *)image{ CALayer *layer = [self createLayerWithImage:image]; [self.layer addSublayer:layer]; [self generateBubbleWithCAlayer:layer];}- (voID)generateBubbleWithCAlayer:(CALayer *)layer{ _maxWIDth = _maxleft + _maxRight + self.bounds.size.wIDth; _startPoint = CGPointMake(self.frame.size.wIDth / 2,0); CGPoint endPoint = CGPointMake(_maxWIDth * [self randomfloat] - _maxleft,-_maxHeight); CGPoint controlPoint1 = CGPointMake(_maxWIDth * [self randomfloat] - _maxleft,-_maxHeight * 0.2); CGPoint controlPoint2 = CGPointMake(_maxWIDth * [self randomfloat] - _maxleft,-_maxHeight * 0.6); CGMutablePathref curvedpath = CGPathCreateMutable(); CGPathMovetoPoint(curvedpath,NulL,_startPoint.x,_startPoint.y); CGPathAddCurvetoPoint(curvedpath,controlPoint1.x,controlPoint1.y,controlPoint2.x,controlPoint2.y,endPoint.x,endPoint.y); CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animation]; keyFrame.keyPath = @"position"; keyFrame.path = CFautorelease(curvedpath); keyFrame.duration = self.duration; keyFrame.calculationMode = kCAAnimationPaced; [layer addAnimation:keyFrame forKey:@"keyframe"]; CABasicAnimation *scale = [CABasicAnimation animation]; scale.keyPath = @"transform.scale"; scale.tovalue = @1; scale.fromValue = [NSValue valueWithCAtransform3D:CAtransform3DMakeScale(0.1,0.1,0.1)]; scale.duration = 0.5; CABasicAnimation *Alpha = [CABasicAnimation animation]; Alpha.keyPath = @"opacity"; Alpha.fromValue = @1; Alpha.tovalue = @0.1; Alpha.duration = self.duration * 0.4; Alpha.beginTime = self.duration - Alpha.duration; CAAnimationGroup *group = [CAAnimationGroup animation]; group.animations = @[keyFrame,scale,Alpha]; group.duration = self.duration; group.delegate = self; group.timingFunction = [camediatimingFunction functionWithname:kcamediatimingFunctionEaSEOut]; group.fillMode = kCAFillModeForwards; group.removedOnCompletion = NO; [layer addAnimation:group forKey:@"group"]; [_array addobject:layer];}- (CGfloat)randomfloat{ return (arc4random() % 100)/100.0f;}- (CALayer *)createLayerWithImage:(UIImage *)image{ CGfloat scale = [UIScreen mainScreen].scale; CALayer *layer = [CALayer layer]; layer.frame = CGRectMake(0,image.size.wIDth / scale,image.size.height / scale); layer.contents = (__brIDge ID)image.CGImage;; return layer;}- (voID)animationDIDStop:(CAAnimation *)anim finished:(BOol)flag{ if (flag) { CALayer *layer = [_array firstObject]; [layer removeAllAnimations]; [layer removeFromSuperlayer]; [_array removeObject:layer]; [_recyclePool addobject:layer]; }}@end
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
总结以上是内存溢出为你收集整理的iOS自定义UIButton点击动画特效全部内容,希望文章能够帮你解决iOS自定义UIButton点击动画特效所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)