葡京网投哪个正规 > 首页 > 使用CAShapeLayer制作类似微信小视频按钮动画,iOS像360滚动圆球那样释放内存

原标题:使用CAShapeLayer制作类似微信小视频按钮动画,iOS像360滚动圆球那样释放内存

浏览次数:147 时间:2019-11-30

iOS设计模式之Target-Action

之前课程代码:

#import"CleanViewController.h"

CALayer动画---使用CAShapeLayer制作类似微信小视频按钮动画,

废话少说,直接上代码。工程截图如下图所示。由于对程序进行了封装,所以在主控制器中,只需要给出该customview的frame即可,显示图形的半径等于给出frame的宽度的一半。

例如: CustomView *customView = [[CustomView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)]; 也就是在位置为(0, 0)处创建出一个半径为100/2=50的view。

图片 1

       图1   工程截图

1 CustomView.m文件中实现的代码

@implementation CustomView

- (instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];

    if (self) {

        //重新设置view的大小 设置为以width为边长的正方形区域

        self.frame = CGRectMake(frame.origin.x

                                , frame.origin.y, frame.size.width, frame.size.width);

        self.layer.cornerRadius = self.frame.size.width/2;

        self.layer.masksToBounds = YES;

        //初始角度

        angle = 0;

        //添加按键

        _button = [UIButton buttonWithType:UIButtonTypeSystem];

        _button.frame = CGRectMake(0, 0, self.frame.size.width, frame.size.width);

        _button.backgroundColor = [UIColor greenColor];

        [self addSubview:_button];

        //添加显示文字的lable

        _lable = [[UILabel alloc]initWithFrame:_button.frame];

        //文字居中

        _lable.textAlignment = NSTextAlignmentCenter;

        _lable.numberOfLines = 0;

        _lable.lineBreakMode = NSLineBreakByWordWrapping;

        _lable.text = @"按住拍";

 

        [self addSubview:_lable];

        

        //添加按键不同事件执行的方法

        [_button addTarget:self action:@selector(cameraButtonTouchDown:) forControlEvents:UIControlEventTouchDown];

        [_button addTarget:self action:@selector(cameraButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

        //绘图

        [self initLayout];

        //定时器设置

        _timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(isButtonClicked) userInfo:nil repeats:YES];

        //暂停定时器

        [_timer setFireDate:[NSDate distantFuture]];

    }

    return self;

}

 

- (void)initLayout{

    

    _shapeLayer = [CAShapeLayer layer];

    _shapeLayer.strokeColor = [UIColor redColor].CGColor;

    _shapeLayer.fillColor = [UIColor clearColor].CGColor;

    _shapeLayer.frame = _button.frame;

    _shapeLayer.lineWidth = 5.0f;

    [self.layer addSublayer:_shapeLayer];

    _bPath = [[UIBezierPath alloc]init];

    //画圆弧

    [_bPath addArcWithCenter:_button.center radius:_button.frame.size.width/2 startAngle:0 endAngle:angle*M_PI/180 clockwise:YES];

    _shapeLayer.path = _bPath.CGPath;

}

#pragma mark 按键被触摸按下 立刻执行方法内的程序

- (void)cameraButtonTouchDown:(UIButton *)sender{

    [_timer setFireDate:[NSDate distantPast]];

    isClicked = YES;

    

}

#pragma mark 按键按下后执行的方法 注意:isClicked = NO 只有在按键弹起后才会生效

- (void)cameraButtonClicked:(UIButton *)sender{

    

    isClicked = NO;

}

#pragma mark 定时器方法 重绘角度等

- (void)isButtonClicked{

    [_shapeLayer removeFromSuperlayer];

    [_bPath removeAllPoints];

    if (isClicked) {

        

        if (angle < 360) {

            angle = angle + 5;

            //重绘

            [self initLayout];

            

            _lable.text = [NSString stringWithFormat:@"已加载%%%ld",angle*100/360];

        }else{

            //关闭定时器

            [_timer setFireDate:[NSDate distantFuture]];

            

            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"拍摄完成" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles: @"确定", nil];

            [alertView show];

            //回到初始状态

            angle = 0;

            _lable.text = @"按住拍";

            [self initLayout];

        }

    }else{

        //关闭定时器

        [_timer setFireDate:[NSDate distantFuture]];

        //回到初始状态

        angle = 0;

        _lable.text = @"按住拍";

        [self initLayout];

    }

}

@end

2 ViewController.m文件中实现的代码

#import "ViewController.h"

#import "CustomView.h"

 

@interface ViewController ()

 

@end

 

@implementation ViewController 

- (void)viewDidLoad {

    [super viewDidLoad];

    CustomView *customView = [[CustomView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];

    customView.center = self.view.center;

    [self.view addSubview:customView];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

@end

3 模拟器运行结果截图

图片 2

 

图片 3

废话少说,直接上代码。工程截图如下图所示。由于对程序进行了封装,所以在...

目标-行为(Target-Action)模式(目的在于让代码解耦合,使代码与代码之间关联性降低,便于后期开发维护)

本次教案:

#import"SDImageCache.h"

Target-action----这个设计模式用按钮,等控件把用户的交互变成代码,让程序可以执行;

Target-action :通俗易懂的说也就是 一个对象包含一些生成一个消息表达式的元素,当一个确定事件出现时,把这些元素放到一起组成消息和发送这个消息。 有些概念是比较抽象模糊的,下面通过一个实例来诠释

提前布置:

#import"UIImageView+WebCache.h"

Target-Action模式

首先先看一下效果图: 图片 4vcHLVGFyZ2V0LUFjdGlvbsSjyr0KPGJyPgoK1NrV4sDvz8i5ub2ov/K83Mu8wrcssrvWwdPav7TG8MC0xMfDtMOjyLs61NrV4sDvxuTKtbrcvPK1pSy31s6qwb249rK91ug6CrXa0rs6tLS9qMj9uPZVSVZpZXe21M/zCrXatv46ubnU7FRhcmdldC1BY3Rpb24oudi8/CkK1NrV4sDv1MvTw7W9wcvL5rv6yas6Csq1z9bL5rv60dXJq7XEt723qDoKCjxwIGNsYXNzPQ=="p1">+ (UIColor *)randomColor

{

return [selfcolorWithRed:arc4random() %256 /255.0green:arc4random() %256 /255.0blue:arc4random() %256 /255.0alpha:1];

}

//第一创建三个UIView对象

代码:

// MHTViewController.m

// LessonTragetAction

// Copyright (c) 2014年 Summer. All rights reserved.

#import"MHTViewController.h"

#import"CustomView.h"

#import"UIColor+RandomColor.h"

#define kCyanView_Frame CGRectMake(100,130, 120, 120)

#define kGreenView_Frame CGRectMake(15,260, 120, 120)

#define kGrayView_Frame CGRectMake(185,260, 120, 120)

@interfaceMHTViewController ()

@end

@implementation MHTViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

self = [super initWithNibName:nibNameOrNilbundle:nibBundleOrNil];

if (self) {

// Custom initialization

}

return self;

}

- (void)viewDidLoad

{

[superviewDidLoad];

// Do any additional setup after loading the view.

self.view.backgroundColor = [UIColoryellowColor];

//view1

CustomView *greenView = [[CustomViewalloc]initWithFrame:kGreenView_Frame];

greenView.backgroundColor = [UIColorgreenColor];

greenView.layer.cornerRadius =60;

[greenViewaddTarget:selfaction:@selector(changeSuperviewColor:)forControlEvents:UIControlEventTouchUpInside];

greenView.layer.masksToBounds =YES;

[self.viewaddSubview:greenView];

[greenViewrelease];

//view2

CustomView *cyanView = [[CustomViewalloc]initWithFrame:kCyanView_Frame];

cyanView.backgroundColor = [UIColorcyanColor];

[self.viewaddSubview:cyanView];

cyanView.layer.cornerRadius =60;

[cyanViewaddTarget:selfaction:@selector(changeSelfColor:)forControlEvents:UIControlEventTouchDown];

cyanView.layer.masksToBounds =YES;

[cyanViewrelease];

//view3

CustomView*grayView = [[CustomViewalloc]initWithFrame:kGrayView_Frame];

grayView.backgroundColor = [UIColorgrayColor];

[self.viewaddSubview:grayView];

[grayViewaddTarget:selfaction:@selector(changeSelfLocation:)forControlEvents:UIControlEventTouchUpInside];

grayView.layer.cornerRadius =60;

grayView.layer.masksToBounds =YES;

[grayViewrelease];

NSArray *titles [email protected][@"更改父视图的颜色" ,@"更改自身的颜色" ,@"更改自身的位置"];

for (int i = 0; i < 3; i++) {

UILabel *lable = [[UILabelalloc]initWithFrame:CGRectMake(0,0,120, 120)];

lable.layer.cornerRadius =60;

lable.layer.masksToBounds =YES;

lable.text = titles[i];

lable.font = [UIFontfontWithName:@"EuphemiaUCAS"size:15];

lable.textAlignment =NSTextAlignmentCenter;

lable.backgroundColor = [UIColorclearColor];

if (0 == i) {

[greenViewaddSubview:lable];

}elseif (1 == i){

[cyanViewaddSubview:lable];

}elseif (2 == i){

[grayViewaddSubview:lable];

}

[lablerelease];

//设置分割线

UILabel *hintLine = [[UILabelalloc]initWithFrame:CGRectMake(50,80,220, 2)];

hintLine.backgroundColor = [UIColorgrayColor];

hintLine.layer.cornerRadius =5;

hintLine.layer.masksToBounds =YES;

[self.viewaddSubview:hintLine];

[hintLinerelease];

//设置功能的提示内容

UILabel *hintLable = [[UILabelalloc]initWithFrame:CGRectMake(50,60,220, 20)];

hintLable.textAlignment =NSTextAlignmentCenter;

hintLable.text [email protected]"文本显示:";

hintLable.font = [UIFontfontWithName:@"AlNile"size:12];

[self.viewaddSubview:hintLable];

[hintLablerelease];

//copyrightLine(版权)

UILabel *copyrightLine = [[UILabelalloc]initWithFrame:CGRectMake(20,520,280, 2)];

copyrightLine.backgroundColor = [UIColorgrayColor];

copyrightLine.layer.cornerRadius =5;

copyrightLine.layer.masksToBounds =YES;

[self.viewaddSubview:copyrightLine];

[copyrightLinerelease];

UILabel *copyrightLable = [[UILabelalloc]initWithFrame:CGRectMake(20,480,280, 20)];

copyrightLable.textAlignment =NSTextAlignmentCenter;

copyrightLable.text [email protected]"Copyright ? 2014 summer";

copyrightLable.font = [UIFontfontWithName:@"AlNile"size:12];

[self.viewaddSubview:copyrightLable];

[copyrightLablerelease];

UILabel *copyrightLable1 = [[UILabelalloc]initWithFrame:CGRectMake(20,500,280, 20)];

copyrightLable1.textAlignment =NSTextAlignmentCenter;

copyrightLable1.text [email protected]"[email protected]";

copyrightLable1.font = [UIFontfontWithName:@"AlNile"size:12];

[self.viewaddSubview:copyrightLable1];

[copyrightLable1release];

}

//修改父视图的颜色

- (void)changeSuperviewColor:(CustomView *)view

{

view.superview.backgroundColor = [UIColorrandomColor];

}

//修改自身的颜色

- (void)changeSelfColor:(CustomView *)view

{

view.backgroundColor = [UIColorrandomColor];

}

//修改自身的位置

- (void)changeSelfLocation:(CustomView *)view

{

view.center =CGPointMake(arc4random() % (260 -60 +1) + 60,arc4random() % (420 -60 + 1) + 60);

}

第二 构建Target-Action

// CustomView.h

// LessonTragetAction

// Copyright (c) 2014年 Summer. All rights reserved.

#import

@interface CustomView :UIView

//创建方法

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;

@end

//*********************

// CustomView.m

// LessonTragetAction

// Copyright (c) 2014年 Summer. All rights reserved.

#import"CustomView.h"

#import"UIColor+RandomColor.h"

@interfaceCustomView()

{

id _target; //目标

SEL _action; //行为

UIControlEvents _controlEvents;

}

@end

@implementation CustomView

- (id)initWithFrame:(CGRect)frame

{

self = [super initWithFrame:frame];

if (self) {

// Initialization code

}

return self;

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

if (UIControlEventTouchDown ==_controlEvents) {

//当当前视图接触到触摸事件之后,交由target去处理

[_targetperformSelector:_actionwithObject:self];

}

}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

{

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

if (UIControlEventTouchUpInside ==_controlEvents) {

[_targetperformSelector:_actionwithObject:self];

}

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

{

}

//为当前视图指定当视图接收到响应事件之后,由target来通过action方法进行响应.

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents

{

//用实例变量存储外界传入的参数,方便在其他方法中使用.

_target = target;

_action = action;

_controlEvents = controlEvents;//触发时机

}

@end

目标-行为(Target-Action)模式(目的在于让代码解耦合,使代码与代码之间关联性降低,便于后期开发维护) Target-act...

要求学生在课下研究iPhone通讯录的拨号页面,仔细观察都有哪些细节实现

@interfaceCleanViewController()

{

实现效果图:

UIImageView*imageV;

图片 5

floattop;

实施步骤

UILabel*_numLabel;

1、基础UIButton实现自定义按钮

UIImageView*warterImgeV ;

2、实现按钮形状,圆形。

float_countFileSize;//所有缓存文件的大小(M)

3、为按钮添加属性(数字和字母标签)

BOOLisClear;

4、为自定义按钮实现动画

UILabel*percentageLabel;

5、实现拨号键盘中的圆形按钮布局

BOOL_isPercentage;

拨号键盘按钮设计及布局分析:

}

图片 6

@end

自定义按钮的实现其实比较简单,只需要实现一个UIButton的派生类就可以了,当然也可以考虑UIControl的子类。

staticfloatW =140;

1、圆形按钮的外功控制

staticfloatH =140;

//设置圆角半径

staticNSIntegeri =0;

self.layer.cornerRadius=self.bounds.size.width/2;

@implementationCleanViewController

//设置子图层蒙版

- (void)viewDidLoad {

self.layer.masksToBounds=YES;

[superviewDidLoad];

//设置按钮的边框宽度和颜色

// Do any additional setup after loading the view.

self.layer.borderWidth=1.0f;

top= [UIScreenmainScreen].bounds.size.height/4.8f;//140;

self.layer.borderColor= [[UIColordarkGrayColor]CGColor];

self.view.backgroundColor= [UIColorcolorWithRed:65/255.fgreen:171/255.fblue:145/255.falpha:1];

/*

//1.创建mainView:

这里CGColor其实是一个结构体,UIColor的一个成员属性

CGRectframe =CGRectMake( ([UIScreenmainScreen].bounds.size.width-W)/2,top,W,H);

可以参考:http://www.cnblogs.com/smileEvday/archive/2012/06/05/UIColor_CIColor_CGColor.html

UIView*view = [[UIViewalloc]initWithFrame:frame];

*/

//view.backgroundColor = [UIColor redColor];

2、动画效果

view.layer.cornerRadius=70;

/*

//view.layer.borderWidth = .13;

在UIKit中,系统提供了animate标题打头的属于UIView的类方法让我们可以轻松的制作动画效果,每一个这样的类方法提供了名为animations的block代码块,这些代码会在方法调用后立刻或者延迟一段时间以动画的方式执行。此外,所有这些API的第一个参数都是用来设置动画时长的。

view.layer.masksToBounds=YES;

*/

[self.viewaddSubview:view];

//触摸开始,播放前半部动画

//view.backgroundColor = [UIColor colorWithRed:65/255.f green:171/255.f blue:145/255.f alpha:1];

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event{

//2.添加外圆环

[supertouchesBegan:toucheswithEvent:event];

CGFloatspace =14;

//在0.5秒钟的时间内透明度由1.0->.0.2

CGRectframeR =CGRectMake( ([UIScreenmainScreen].bounds.size.width-W)/2-space/2,top-space/2,W+space,H+space);

[UIViewanimateWithDuration:0.5fanimations:^{

imageV=[[UIImageViewalloc]initWithFrame:frameR];

self.alpha=0.2f;

imageV.image= [UIImageimageNamed:@"fb_rotation"];

}];

[self.viewaddSubview:imageV];

}

[NSTimerscheduledTimerWithTimeInterval:.1target:selfselector:@selector(timeActR:)userInfo:imageVrepeats:YES];

//触摸结束,播放后半段动画

//添加图片水;

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent*)event{

warterImgeV= [[UIImageViewalloc]initWithFrame:CGRectMake(0,10,500,250)];

[supertouchesEnded:toucheswithEvent:event];

warterImgeV.image= [UIImageimageNamed:@"fb_wave.png"];

[UIViewanimateWithDuration:0.5fanimations:^{

//warterImgeV.backgroundColor = [UIColor yellowColor];

self.alpha=1.0f;

[viewaddSubview:warterImgeV];

}];

warterImgeV.transform=CGAffineTransformMakeTranslation(-350,100);

}

//2.5添加百分比label:percentage百分比

3、属性标签的实现:

percentageLabel= [[UILabelalloc]initWithFrame:CGRectMake(0,0, view.frame.size.width, view.frame.size.height)];

数字键1,只显示一个标签,但是位置靠上

//percentageLabel.text = @"20.3%";

*、#、拨号键只显示一个标签,且居中

percentageLabel.text= [NSStringstringWithFormat:@"%.01f%%",arc4random_uniform(20)+0.2f];

数字键1,只显示一个标签,但是位置靠上

percentageLabel.textColor= [UIColorredColor];

图片 7

percentageLabel.font= [UIFontsystemFontOfSize:36weight:7];

总结,可以分为两类:

percentageLabel.textAlignment=NSTextAlignmentCenter;

有子标签:所有数字键(包括“1”,可以将其看作子标签不显示的特殊情况): self.subTitle!=nil

[viewaddSubview:percentageLabel];

没有子标签的:符号键(*、#)和拨号键(这里不用图标,使用字符“call”): self.subTitle==nil

//2.6创建百分比label

使用分支语句,兼容两种按钮(

UILabel*label = [[UILabelalloc]initWithFrame:CGRectMake(0,percentageLabel.frame.size.height/2,percentageLabel.frame.size.width,percentageLabel.frame.size.height/2)];

if(subTitle !=nil){

label.text=@"百分比";

//添加属性标签:

label.font= [UIFontsystemFontOfSize:18weight:4];

UILabel*mainLabel = [[UILabelalloc]initWithFrame:CGRectMake(0,0,self.bounds.size.width,self.bounds.size.height/3.0*2)];

label.textAlignment=NSTextAlignmentCenter;

mainLabel.text=self.mainTitle;

label.textColor= [UIColorredColor];

[mainLabelsetTextAlignment:NSTextAlignmentCenter];

[percentageLabeladdSubview:label];

[mainLabelsetTextColor:[UIColordarkGrayColor]];

//3.添加横线

[mainLabelsetFont:[UIFontfontWithName:@"Heiti SC"size:40.0f]];

//3.添加横线

[selfaddSubview:mainLabel];

UIView*blackView = [[UIViewalloc]initWithFrame:CGRectMake(30,CGRectGetMaxY(view.frame)+50,[UIScreenmainScreen].bounds.size.width-30*2,8)];

//将mainLabel的中心点移至水平中线1/3点

blackView.backgroundColor= [UIColorblackColor];

mainLabel.center=CGPointMake(self.bounds.size.width/2,self.bounds.size.height/3.0+5);

blackView.layer.cornerRadius=4;

UILabel*subLabel = [[UILabelalloc]initWithFrame:CGRectMake(0,0,self.bounds.size.width,self.bounds.size.height/3.0)];

blackView.layer.masksToBounds=YES;

subLabel.text=self.subTitle;

[self.viewaddSubview:blackView];

[subLabelsetTextAlignment:NSTextAlignmentCenter];

//4.创建label

[subLabelsetTextColor:[UIColordarkGrayColor]];

_numLabel= [[UILabelalloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(blackView.frame)+20,[UIScreenmainScreen].bounds.size.width,55)];

//Helvetica字体是西方最常用的一种字体

//_numLabel.backgroundColor = [UIColor redColor];

[subLabelsetFont:[UIFontfontWithName:@"Helvetica"size:10.0f]];

_numLabel.textColor= [UIColorredColor];

//将subLabel的中心点移至水平中线2/3点

_numLabel.text=@"缓存:999933399M";

subLabel.center=CGPointMake(self.bounds.size.width/2.0,self.bounds.size.height/3*2);

_numLabel.textAlignment=NSTextAlignmentCenter;

[selfaddSubview:subLabel];

_numLabel.font= [UIFontsystemFontOfSize:32weight:9];

}else{

[self.viewaddSubview:_numLabel];

//            self.titleLabel.text = self.mainTitle;

[selfcountCacheFileSize];//设置文件大小

[selfsetTitle:self.mainTitleforState:UIControlStateNormal];

//5.创建清除按钮

[selfsetTitleColor:[UIColordarkGrayColor]forState:UIControlStateNormal];

UIButton*btn = [[UIButtonalloc]initWithFrame:CGRectMake(( [UIScreenmainScreen].bounds.size.width-150)/2,CGRectGetMaxY(_numLabel.frame)+20,150,40)];

[self.titleLabelsetFont:[UIFontfontWithName:@"Heiti SC"size:40.0f]];

btn.backgroundColor= [UIColorcolorWithRed:0green:0blue:0alpha:.2];

//            [self.titleLabel setTextColor:[UIColor darkGrayColor]];

[btnsetTitle:@"一键清除"forState:UIControlStateNormal];

}

[btnsetTitleColor:[UIColorredColor]forState:UIControlStateNormal];

4、事件处理,代码同上。

[btnsetTitleColor:[UIColorwhiteColor]forState:UIControlStateSelected];

老版本中需发现touchesBegin:和touchesEnded:方法与AddTargart:产生冲突,比较理想的解决办法是自己实现一个类似addTarget的方法,这李没有直接覆盖是因为文档中并没有addTarget方法的详细说明,所以折中自己实现,代码如下:

btn.layer.cornerRadius=10;

新版本中发现已经不存在这个问题,所以可以直接使用父类中继承的addTarget方法,因此,处理机制和普通方法没有任何区别。

btn.layer.masksToBounds=YES;

本模块项目完整代码如下:

btn.layer.borderColor= [UIColorwhiteColor].CGColor;

MPCircleButton.h

btn.layer.borderWidth=.5;

//

[btnaddTarget:selfaction:@selector(btnAct:)forControlEvents:UIControlEventTouchUpInside];

//  MPCircleButton.h

[btnsetTitle:@"完成/退出"forState:UIControlStateSelected];

//仿iPhone电话App_2017

[self.viewaddSubview:btn];

//

}

//  Created by GuoChunlei on 2017/6/26.

//按钮的点击方法:

//  Copyright © 2017年class3g. All rights reserved.

-(void)btnAct:(UIButton*)btn{

//

//1.一键清除

#import

if(btn.selected==NO) {

@interfaceMPCircleButton :UIButton

_isPercentage= !_isPercentage;//设置启动百分比加+

//主标签:数字、*、#、Call

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.1*NSEC_PER_SEC)),dispatch_get_main_queue(), ^{

@property(nonatomic,strong)NSString*mainTitle;

[UIViewanimateWithDuration:3animations:^{

//子标签:字母

warterImgeV.transform=CGAffineTransformIdentity;

@property(nonatomic,strong)NSString*subTitle;

}completion:^(BOOLfinished) {

//设置属性,初始化外观

//做动画效果:

-(void)setMainTitle:(NSString*)mainTitle andSubTitle:(NSString*) subTitle;

[NSTimerscheduledTimerWithTimeInterval:1target:selfselector:@selector(timeAct:)userInfo:warterImgeVrepeats:YES];

@end

}];

MPCircleButton.m

});

//

// 4、清除所有的缓存文件

//  MPCircleButton.m

//-----导入清除缓存-------

//仿iPhone电话App_2017

[[SDImageCachesharedImageCache]clearDisk];

//

isClear=YES;

//  Created by GuoChunlei on 2017/6/26.

//[self countCacheFileSize];

//  Copyright © 2017年class3g. All rights reserved.

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3*NSEC_PER_SEC)),dispatch_get_main_queue(), ^{

//

//清除缓存:

#import"MPCircleButton.h"

[selfcountCacheFileSize];

@implementationMPCircleButton

});

-(void)setMainTitle:(NSString*)mainTitle andSubTitle:(NSString*) subTitle{

//2.完成推出

if(self!=nil) {

}else{

self.mainTitle= mainTitle;

[selfdismissViewControllerAnimated:NOcompletion:^{

self.subTitle= subTitle;

}];

//设置圆角半径

}

self.layer.cornerRadius=self.bounds.size.width/2;

btn.selected= !btn.selected;

//设置子图层蒙版

}

self.layer.masksToBounds=YES;

//定时器切换

//设置按钮的边框宽度和颜色

-(void)timeAct:(NSTimer*)timer{

self.layer.borderWidth=1.0f;

//UIImageView *warterImgeV = timer.userInfo;

self.layer.borderColor= [[UIColordarkGrayColor]CGColor];

CGFloatYi =7;

/*

if(i==0) {

这里CGColor其实是一个结构体,UIColor的一个成员属性

//重新设置frame:

可以参考:http://www.cnblogs.com/smileEvday/archive/2012/06/05/UIColor_CIColor_CGColor.html

CGRectfloatCont =warterImgeV.frame;

*/

floatCont.origin.x= -CGRectGetWidth(warterImgeV.frame)+W+Yi;

if(subTitle !=nil){

warterImgeV.frame= floatCont;

//添加属性标签:

}

UILabel*mainLabel = [[UILabelalloc]initWithFrame:CGRectMake(0,0,self.bounds.size.width,self.bounds.size.height/3.0*2)];

[UIViewanimateWithDuration:3.2animations:^{

mainLabel.text=self.mainTitle;

warterImgeV.transform=CGAffineTransformMakeTranslation(CGRectGetWidth(warterImgeV.frame)-1.3*W,0);

[mainLabelsetTextAlignment:NSTextAlignmentCenter];

[timersetFireDate:[NSDatedistantFuture]];

[mainLabelsetTextColor:[UIColordarkGrayColor]];

}completion:^(BOOLfinished) {

[mainLabelsetFont:[UIFontfontWithName:@"Heiti SC"size:40.0f]];

[timersetFireDate:[NSDatedistantPast]];

[selfaddSubview:mainLabel];

warterImgeV.transform=CGAffineTransformIdentity;

//将mainLabel的中心点移至水平中线1/3点

}];

mainLabel.center=CGPointMake(self.bounds.size.width/2,self.bounds.size.height/3.0+5);

}

UILabel*subLabel = [[UILabelalloc]initWithFrame:CGRectMake(0,0,self.bounds.size.width,self.bounds.size.height/3.0)];

-(void)timeActR:(NSTimer*)timer{

subLabel.text=self.subTitle;

//2.设置旋转:

[subLabelsetTextAlignment:NSTextAlignmentCenter];

imageV.transform=CGAffineTransformRotate(imageV.transform,M_PI/12);

[subLabelsetTextColor:[UIColordarkGrayColor]];

staticCGFloaty =21;

//Helvetica字体是西方最常用的一种字体

if(_isPercentage){

[subLabelsetFont:[UIFontfontWithName:@"Helvetica"size:10.0f]];

if(y<91) {

//将subLabel的中心点移至水平中线2/3点

percentageLabel.text= [NSStringstringWithFormat:@"%.01f%%",y];

subLabel.center=CGPointMake(self.bounds.size.width/2.0,self.bounds.size.height/3*2);

y=y+4.1;

[selfaddSubview:subLabel];

}

}else{

}

//            self.titleLabel.text = self.mainTitle;

}

[selfsetTitle:self.mainTitleforState:UIControlStateNormal];

//------导入清除缓存----------

[selfsetTitleColor:[UIColordarkGrayColor]forState:UIControlStateNormal];

-(void)countCacheFileSize{

[self.titleLabelsetFont:[UIFontfontWithName:@"Heiti SC"size:40.0f]];

//float _countFileSize; //所有缓存文件的大小(M)

//            [self.titleLabel setTextColor:[UIColor darkGrayColor]];

// 1、找到缓存文件的保存路径

}

// NSHomeDirectory()找到应用的沙盒路径

}

NSString*filePath =[NSHomeDirectory()stringByAppendingPathComponent:@"Library/Caches/com.hackemist.SDWebImageCache.default"];

}

NSLog(@"->%@",filePath);

/*

// 2、获取所有的缓存文件

在UIKit中,系统提供了animate标题打头的属于UIView的类方法让我们可以轻松的制作动画效果,每一个这样的类方法提供了名为animations的block代码块,这些代码会在方法调用后立刻或者延迟一段时间以动画的方式执行。此外,所有这些API的第一个参数都是用来设置动画时长的。

//文件管理对象类NSFileManager,有它去管理我们的文件目录

*/

NSFileManager*fileManager = [NSFileManagerdefaultManager];

//触摸开始,播放前半部动画

// subpathsOfDirectoryAtPath,获取当前路径下所有子文件名

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event{

NSArray*fileArray = [fileManagersubpathsOfDirectoryAtPath:filePatherror:nil];

[supertouchesBegan:toucheswithEvent:event];

// 3、计算所有缓存文件的大小

//在0.5秒钟的时间内透明度由1.0->.0.2

longlongfileSize =0;//所有文件的总字节数

[UIViewanimateWithDuration:0.5fanimations:^{

for(NSString*fileNameinfileArray) {

self.alpha=0.2f;

//获得每一个缓存文件的路径

}];

NSString*subFilePath = [filePathstringByAppendingPathComponent:fileName];

}

NSDictionary*attributes = [fileManagerattributesOfItemAtPath:subFilePatherror:nil];

//触摸结束,播放后半段动画

//NSLog(@"文件属性:%@",attributes);

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent*)event{

//每个子文件所占的字节大小

[supertouchesEnded:toucheswithEvent:event];

longlongsize =[attributes[NSFileSize]longLongValue];

[UIViewanimateWithDuration:0.5fanimations:^{

fileSize += size;

self.alpha=1.0f;

}

}];

//转为一兆(M)为单位

}

_countFileSize= fileSize /1024.0/1024.0;

//-(void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents{

NSLog(@"文件的储存大小:%f,,,,%lld",_countFileSize,fileSize);

//    [super addTarget:target action:action forControlEvents:controlEvents];

//

//

//if (isClear == YES) {

//

//NSString *count = [NSString stringWithFormat:@"%.0f", _countFileSize];

//}

//if ([count isEqualToString:@"0"]) {

@end

//[MBProgressHUD showMessage:@"清理完成" view:self.view];

DialPadViewController.m:测试代码

//isClear = NO;

//

//}else{

//  DialPadViewController.m

//[self countCacheFileSize];

//仿iPhone通讯录

//}

//

//}

//  Created by GuoChunlei on 2017/6/26.

//

//  Copyright © 2017年class3g. All rights reserved.

//return _countFileSize;

//

_numLabel.text= [NSStringstringWithFormat:@"缓存:%04fM",_countFileSize];

#import"DialPadViewController.h"

}

#import"MPCircleButton.h"

图片 8

@interfaceDialPadViewController()

图片 9

@end

@implementationDialPadViewController

- (void)viewDidLoad {

[superviewDidLoad];

// Do any additional setup after loading the view.

MPCircleButton*testBtn = [[MPCircleButtonalloc]initWithFrame:CGRectMake(100,200,100,100)];

[self.viewaddSubview:testBtn];

[testBtnsetMainTitle:@"2"andSubTitle:@"ABC"];

testBtn.backgroundColor= [UIColororangeColor];

[testBtnaddTarget:selfaction:@selector(onclick:)forControlEvents:UIControlEventTouchUpInside];

MPCircleButton*otherTestBtn = [[MPCircleButtonalloc]initWithFrame:CGRectMake(100,400,100,100)];

[self.viewaddSubview:otherTestBtn];

[otherTestBtnsetMainTitle:@"#"andSubTitle:nil];

otherTestBtn.backgroundColor= [UIColorgreenColor];

//    otherTestBtn setTitle:<#(nullable NSString *)#> forState:<#(UIControlState)#>

[otherTestBtnaddTarget:selfaction:@selector(onOtherClick:)forControlEvents:UIControlEventTouchUpInside];

}

-(void)onclick:(MPCircleButton*)btn{

NSLog(@"%@", btn.mainTitle);

}

-(void)onOtherClick:(MPCircleButton*)btn{

NSLog(@"%@", btn.mainTitle);

}

- (void)didReceiveMemoryWarning {

[superdidReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

/*

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

// Get the new view controller using [segue destinationViewController].

// Pass the selected object to the new view controller.

}

*/

@end

本课习题:

1、完成课程中讲述的拨号键盘全部功能

2、研究尝试实现号码显示标签,如下图所示

图片 10

本文由葡京网投哪个正规发布于首页,转载请注明出处:使用CAShapeLayer制作类似微信小视频按钮动画,iOS像360滚动圆球那样释放内存

关键词:

上一篇:调用拨打电话功能,拨打电话的两种方式

下一篇:iOS使用ffmpeg播放rstp实时监控视频数据流,搞了一周葡京网投哪个正规: