最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS自定義UIButton點擊動畫特效

 更新時間:2019年04月28日 14:19:53   作者:hero_wqb  
這篇文章主要為大家詳細介紹了iOS自定義UIButton點擊動畫特效,具有一定的參考價值,感興趣的小伙伴們可以參考一下

借鑒相關資料,整理了一個很有意思的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];
 
  //創(chuàng)建控件
  [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, NULL, 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, 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

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • iOS開發(fā)之通過銀行卡號獲取所屬銀行名稱

    iOS開發(fā)之通過銀行卡號獲取所屬銀行名稱

    本文給大家分享一段代碼關于ios通過銀行卡號獲取所屬銀行名稱,代碼簡單易懂,在項目開發(fā)中經(jīng)常會遇到這樣的功能,需要的朋友一起學習吧
    2016-11-11
  • iOS圖片拉伸技巧小結(jié)

    iOS圖片拉伸技巧小結(jié)

    這篇文章主要為大家詳細介紹了iOS圖片拉伸的技巧,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • objective-c實現(xiàn)點到直線的距離及與垂足的交點

    objective-c實現(xiàn)點到直線的距離及與垂足的交點

    這篇文章主要給大家介紹了利用objective-c實現(xiàn)點到直線的距離及與垂足的交點的相關資料,文中給出了詳細的實現(xiàn)思路和實現(xiàn)代碼,對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。
    2017-04-04
  • iOS中設置view圓角化的四種方法示例

    iOS中設置view圓角化的四種方法示例

    最近因為工作的原因,遇到view圓角優(yōu)化的問題,所以將實現(xiàn)的幾種方法總結(jié)分享出來,下面這篇文章主要給大家介紹了關于iOS中設置view圓角化的四種方法,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起學習學習吧。
    2017-09-09
  • IOS UIView的生命周期的實例詳解

    IOS UIView的生命周期的實例詳解

    這篇文章主要介紹了IOS UIView的生命周期的實例詳解的相關資料,希望通過本文大家能掌握理解這部分內(nèi)容,需要的朋友可以參考下
    2017-09-09
  • iOS 隱藏tabbar代碼詳解

    iOS 隱藏tabbar代碼詳解

    這篇文章主要介紹了iOS 隱藏tabbar代碼詳解的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-07-07
  • iOS開發(fā)教程之APP內(nèi)部切換語言的實現(xiàn)方法

    iOS開發(fā)教程之APP內(nèi)部切換語言的實現(xiàn)方法

    這篇文章主要給大家介紹了關于iOS開發(fā)教程之APP內(nèi)部切換語言的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-02-02
  • iOS利用NSAttributedString實現(xiàn)圖文混排效果示例

    iOS利用NSAttributedString實現(xiàn)圖文混排效果示例

    iOS7以后,因為TextKit的強大,可以用NSAttributedString很方便的實現(xiàn)圖文混排(主要是利用了NSTextAttachment),所以下面這篇文章主要給大家介紹了關于iOS利用NSAttributedString實現(xiàn)圖文混排效果的相關資料,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-10-10
  • iOS UIView常見屬性方法小結(jié)

    iOS UIView常見屬性方法小結(jié)

    本文通過實例代碼給大家詳細介紹了iOS UIView常見屬性方法,非常不錯,需要的朋友參考下吧
    2016-12-12
  • iOS開發(fā)中CALayer使用的基本教程

    iOS開發(fā)中CALayer使用的基本教程

    這篇文章主要介紹了iOS開發(fā)中CALayer使用的基本教程,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下
    2015-11-11

最新評論

太仆寺旗| 五原县| 建始县| 扎囊县| 体育| 合川市| 江城| 南汇区| 海南省| 喜德县| 达日县| 枣阳市| 信丰县| 芦溪县| 芜湖市| 涟水县| 原阳县| 康定县| 驻马店市| 昭觉县| 华亭县| 开阳县| 涟源市| 宝丰县| 武鸣县| 瑞金市| 齐齐哈尔市| 虞城县| 广灵县| 南华县| 建德市| 白河县| 桐庐县| 贡觉县| 无锡市| 梓潼县| 嘉善县| 江安县| 朝阳市| 游戏| 方城县|