美文网首页
扇形动画

扇形动画

作者: 晶宝的小花园 | 来源:发表于2017-02-11 22:04 被阅读0次

新建UIview的类:paintview

//  PieView.h

#import <UIKit/UIKit.h>

@interface PieView : UIView

- (id)initWithFrame:(CGRect)frame;
- (void)stroke;

@end

//  PieView.m

#import "PieView.h"
#define kAnimationDuration 3.0f

#define kPieBackgroundColor [UIColor grayColor]
#define kPieFillColor [UIColor clearColor].CGColor
#define kPieRandColor [UIColor orangeColor].CGColor
#define kLabelLoctionRatio (1.2*bgRadius)

@interface PieView()

@property (nonatomic) CAShapeLayer *bgLayer;

@end


@implementation PieView

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.hidden = YES;
        
        
        //1.pieView中心点
        CGFloat centerWidth = self.frame.size.width * 0.5f;
        CGFloat centerHeight = self.frame.size.height * 0.5f;
        CGFloat centerX = centerWidth;
        CGFloat centerY = centerHeight;
        CGPoint centerPoint = CGPointMake(centerX, centerY);
        CGFloat radiusBasic = centerWidth > centerHeight ? centerHeight : centerWidth;
        
        
        
        
        //2.路径
        
               
        
        
        //背景路径
        CGFloat bgRadius = radiusBasic * 0.5;
        UIBezierPath *bgPath = [UIBezierPath bezierPathWithArcCenter:centerPoint radius:bgRadius   startAngle:-M_PI_2     endAngle:M_PI_2 * 3  clockwise:YES];
        CAShapeLayer* bgLayer = [CAShapeLayer layer];
        bgLayer.fillColor   = [UIColor clearColor].CGColor;
        bgLayer.strokeColor = [UIColor lightGrayColor].CGColor;
        bgLayer.zPosition   = 1;
        bgLayer.lineWidth   = bgRadius * 2.0f;//线宽是扇形半径*2
        bgLayer.path        = bgPath.CGPath;
        self.bgLayer = bgLayer;
        
 
        //pie路径
        CGFloat pieRadius = radiusBasic * 0.5;
        UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:centerPoint radius:pieRadius startAngle:-M_PI/2 endAngle:M_PI*3/2 clockwise:YES];
        
        CAShapeLayer *pie = [CAShapeLayer layer];
        pie.fillColor   = kPieFillColor;
        pie.strokeColor = kPieRandColor;
        pie.lineWidth   = pieRadius * 2.0f;//线宽是扇形半径*2
        pie.zPosition   = 2;
        pie.path        = path.CGPath;
            
        [self.layer addSublayer:pie];
        
        self.layer.mask = bgLayer;
        
    }
    return self;
}


- (void)stroke
{
    //画图动画
    self.hidden = NO;
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    animation.duration  = kAnimationDuration;
    animation.fromValue = @0.0f;
    animation.toValue   = @1.0f;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
 animation.removedOnCompletion = YES;
    [self.bgLayer addAnimation:animation forKey:@"circleAnimation"];
    
}

- (void)dealloc
{
    [self.layer removeAllAnimations];
}

@end

//
//  ViewController.m
//  xiazai
//
//  Created by chenvinci on 2017/2/10.
//  Copyright © 2017年 cuijing. All rights reserved.
//

#import "ViewController.h"
#import "PieView.h"
#define kScreenWidth [UIScreen mainScreen].bounds.size.width



//#define width [UIScreen mainScreen].bounds.size.width
#define height [UIScreen mainScreen].bounds.size.height
#define centerX width/2
#define centerY height/2
#define radius1 100



@interface ViewController ()
@property(nonatomic) PieView*pie;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.pie = [[PieView alloc] initWithFrame:CGRectMake((kScreenWidth - 200) * 0.5f, 100, 200, 200) ];
    [self.view addSubview:self.pie];
    

    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.pie stroke];
    
  }

相关文章

  • 扇形动画

    新建UIview的类:paintview

  • 【Android 开发】自定义View(中)——基本图像绘制

    内容简概 一、画线、圆、扇形二、扇形动画 具体内容 一、画线、圆、扇形、圆弧 (一)创建一个类(继承View)用于...

  • iOS - 扇形计时动画

    第一次使用简书,记录下平时写的demo, 因为最近时间比较闲,所以找了些网上的效果下载下来学习下,首先说明这是从别...

  • iOS开发学习-扇形动画

    前言:最近比较闲,正好利用这段时间把现在项目用的东西封装一下,方便以后复用,当然好的东西还是要分享。一起学习,一起...

  • iOS 扇形图动画效果

    动画效果如图所示: 这天儿是真特么热啊,现在的基本状态就是:晚上热的睡不着、早上困得醒不来、白天工作没精神……照这...

  • 简单扇形动画的应用

    今天项目遇到一个场景:播放PPT给用户看。功能已经做好了,但是每次测试都很痛苦,这个PPT要播放多久啊?还有多少页...

  • ArcMenu

    仿Path带动画效果的扇形菜单 用法 改变外观可以在xml中 或者在java中 ArcMenu

  • 扇形比例图

    通过自定义layer属性动画完成扇形渲染。 代码下载地址:http://code.cocoachina.com/v...

  • 带动画的扇形导航栏

    效果: 源码: 目的: 点击封面导航(最上层的)后展开,最底下的导航(第一个li)处于水平位置,相邻的导航夹角成1...

  • vue中用canvas撸一个抽奖

    要实现的功能 分割扇形 图片文字居中 图片最大尺寸限制 动画 缓动 步骤 config → init → load...

网友评论

      本文标题:扇形动画

      本文链接:https://www.haomeiwen.com/subject/ckboittx.html