国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

視圖收起動畫

2019-11-09 17:20:49
字體:
來源:轉載
供稿:網友

最近在寫項目集成即時通訊,音視頻修改成類似QQ中收起的動畫效果:

動畫代碼比較簡單,效果:

動畫View添加到window中為了方便可以不管到什么控制器都可以懸浮展示,

根據demo大致寫了下代碼:

//

//  ShowContentView.h

#import <UIKit/UIKit.h>

#import <AVFoundation/AVFoundation.h>

@interface ShowContentView :UIView<CAAnimationDelegate>

@PRoperty (nonatomic,strong)UIView *headView;

@property (nonatomic,strong)UIButton *clicBtn;

/** 動畫用的layer */

@property (strong,nonatomic)CAShapeLayer *shapeLayer;

@property (nonatomic,strong)UIButton *smallBtn;

@end

//

//  ShowContentView.m

#import "ShowContentView.h"

@implementation ShowContentView

- (instancetype)initWithFrame:(CGRect)frame

{

    if (self = [superinitWithFrame:frame]) {

        UIView *headView = [[UIViewalloc]initWithFrame:CGRectMake(0,0, 120,120)];

        headView.center =CGPointMake(self.center.x,130);

        [headView setBackgroundColor:[UIColorredColor]];

        headView.layer.cornerRadius =60;

        headView.clipsToBounds =YES;

        self.clipsToBounds =YES;

        self.headView = headView;

        [selfaddSubview:headView];

        

        UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeCustom];

        btn.frame =CGRectMake(30, [UIScreenmainScreen].bounds.size.height-80, [UIScreen mainScreen].bounds.size.width - 60, 30);

        [btn setBackgroundColor:[UIColorredColor]];

        [btn setTitle:@"點我"forState:UIControlStateNormal];

        self.clicBtn = btn;

        [btn addTarget:selfaction:@selector(btnclickAction:)forControlEvents:UIControlEventTouchUpInside];

        [selfaddSubview:btn];

    }

    returnself;

}

- (void)btnclickAction:(UIButton *)btn

{

    //開始的圓形

    UIBezierPath *endPath = [UIBezierPathbezierPathWithOvalInRect:self.headView.frame];

    CGSize startSize =CGSizeMake(self.frame.size.width * 0.5, self.frame.size.height-self.headView.center.y);

    CGFloat radius =sqrt(startSize.width*startSize.width+startSize.height*startSize.height);

    CGRect statF =CGRectInset(self.headView.frame, -radius, -radius);

    UIBezierPath *startPath = [UIBezierPathbezierPathWithOvalInRect:statF];

    

    CAShapeLayer *shapLayer = [CAShapeLayerlayer];

    shapLayer.path = endPath.CGPath;

    self.layer.mask = shapLayer;

    self.shapeLayer = shapLayer;

    //動畫

    CABasicAnimation *pathAnimation = [CABasicAnimationanimationWithKeyPath:@"path"];

    pathAnimation.fromValue = (id)startPath.CGPath;

    pathAnimation.toValue = (id)endPath.CGPath;

    pathAnimation.duration =0.5;

    pathAnimation.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseInEaSEOut];

    pathAnimation.delegate =self;

    pathAnimation.removedOnCompletion =NO;

    pathAnimation.fillMode =kCAFillModeForwards;

    

    [shapLayer addAnimation:pathAnimationforKey:@"shapAnmination"];

}

#pragma mark - CAAnimationDelegate

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag

{

    if ([animisEqual:[self.shapeLayeranimationForKey:@"shapAnmination"]]) {

        CGRect rect =self.frame;

        rect.origin =self.headView.frame.origin;

        self.bounds = rect;

        rect.size =self.headView.frame.size;

        self.frame = rect;

        

        [UIViewanimateWithDuration:1.0animations:^{

            self.center =CGPointMake([UIScreenmainScreen].bounds.size.width - 60, [UIScreenmainScreen].bounds.size.height - 80);

            self.transform =CGAffineTransformMakeScale(0.5,0.5);

            

        } completion:^(BOOL finished) {

            self.smallBtn.frame =self.frame;

            self.smallBtn.layer.cornerRadius = self.smallBtn.bounds.size.width * 0.5;

            self.smallBtn.layer.masksToBounds = YES;

            [self.superviewaddSubview:_smallBtn];

        }];

    } elseif ([anim isEqual:[self.shapeLayeranimationForKey:@"showAnimation"]]) {

        self.layer.mask =nil;

        self.shapeLayer =nil;

    }

}

- (UIButton *)smallBtn

{

    if (!_smallBtn) {

        _smallBtn = [UIButtonbuttonWithType:UIButtonTypeCustom];

        [_smallBtnaddTarget:selfaction:@selector(microClick)forControlEvents:UIControlEventTouchUpInside];

    }

    return_smallBtn;

}

- (void)microClick

{

    [self.smallBtnremoveFromSuperview];

    self.smallBtn =nil;

    

    [UIViewanimateWithDuration:1.0animations:^{

        self.center =self.headView.center;

        self.transform =CGAffineTransformIdentity;

    } completion:^(BOOL finished) {

        self.bounds = [UIScreenmainScreen].bounds;

        self.frame =self.bounds;

        

        CAShapeLayer *shapeLayer =self.shapeLayer;

        

        // 1.獲取動畫縮放開始時的圓形

        UIBezierPath *startPath = [UIBezierPathbezierPathWithOvalInRect:self.headView.frame];

        

        // 2.獲取動畫縮放結束時的圓形

        CGSize endSize =CGSizeMake(self.frame.size.width * 0.5, self.frame.size.height - self.headView.center.y);

        CGFloat radius =sqrt(endSize.width * endSize.width + endSize.height * endSize.height);

        CGRect endRect =CGRectInset(self.headView.frame, -radius, -radius);

        UIBezierPath *endPath = [UIBezierPathbezierPathWithOvalInRect:endRect];

        

        // 3.創建shapeLayer作為視圖的遮罩

        shapeLayer.path = endPath.CGPath;

        

        // 添加動畫

        CABasicAnimation *pathAnimation = [CABasicAnimationanimationWithKeyPath:@"path"];

        pathAnimation.fromValue = (id)startPath.CGPath;

        pathAnimation.toValue = (id)endPath.CGPath;

        pathAnimation.duration =0.5;

        pathAnimation.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseInEaseOut];

        pathAnimation.delegate =self;

        pathAnimation.removedOnCompletion =NO;

        pathAnimation.fillMode =kCAFillModeForwards;

        

        [shapeLayer addAnimation:pathAnimationforKey:@"showAnmination"];

    }];

}

@end

//

//  ViewController.m

//

#import "ViewController.h"

#import "ShowContentView.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    [self.viewsetBackgroundColor:[UIColorgreenColor]];

    ShowContentView *showView = [[ShowContentViewalloc]initWithFrame:self.view.frame];

    [showView setBackgroundColor:[UIColorlightGrayColor]];

    [self.viewaddSubview:showView];

}

@end


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 日土县| 营口市| 峨边| 德化县| 武鸣县| 凤城市| 高平市| 子洲县| 长泰县| 蓬溪县| 土默特左旗| 浙江省| 鲁山县| 上饶县| 常州市| 大洼县| 无极县| 茶陵县| 江源县| 青冈县| 花莲市| 清水河县| 调兵山市| 中西区| 五常市| 江永县| 安国市| 湖州市| 三门县| 新津县| 明溪县| 周宁县| 含山县| 界首市| 无锡市| 永胜县| 吉木萨尔县| 清镇市| 长岭县| 高邮市| 沈阳市|