利用UIBezierPath绘制加载动画哦

这个博客展示了如何利用UIBezierPath和CAShapeLayer在iOS中创建一个自定义的加载动画效果。通过创建一个名为ZJProgressHUD的视图类,实现了旋转动画和文字加载动画,并提供了显示和隐藏的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//  Created by mac on 16/12/23.
//  Copyright © 2016年 zhangjian. All rights reserved.
//
// http://blog.sina.com.cn/resoftios
#import

@interface ZJProgressHUD : UIView

@property (nonatomic , copy) NSString *tipText;

@property (nonatomic , strong) UIColor *toastColor;

@property (nonatomic , strong) UIColor *contentColor;

@property (nonatomic , assign) BOOL showMask;

- (void)show:(BOOL)animated;

- (void)hide:(BOOL)animated;


+ (instancetype)showHUDto:(UIView *)view animated:(BOOL)animated;

+ (NSUInteger)hideAllHUDsForView:(UIView *)view animated:(BOOL)animated;
// http://blog.sina.com.cn/resoftios
@end


//  Created by mac on 16/12/23.
//  Copyright © 2016年 zhangjian. All rights reserved.
//
#import "ZJProgressHUD.h"
#import

#define SIZE_RADIUS_WIDTH 12
#define SIZE_FONT_TIP 12

#define TIP_DEFULT_TEXT  @"Loading..."

#define KEY_ANIMATION_ROTATE @"KEY_ANIMATION_ROTATE"
#define KEY_ANIMATION_TEXT @"KEY_ANIMATION_TEXT"

@interface ZJProgressHUD()

@property (nonatomic , strong) UIView *toast;

@property (nonatomic , strong) UIView *rotateView;

@property (nonatomic , strong) CAShapeLayer *rotateLayer;

@property (nonatomic , strong) CAShapeLayer *textLayer;
@end

@implementation ZJProgressHUD

+ (instancetype)showHUDto:(UIView *)view animated:(BOOL)animated
{
      ZJProgressHUD *hud = [[self alloc] initWithView:view];
      [view addSubview:hud];
      [hud show:animated];
      return hud;
}

+ (NSUInteger)hideAllHUDsForView:(UIView *)view animated:(BOOL)animated
{
      NSMutableArray *huds = [NSMutableArray array];
      NSArray *subviews = view.subviews;
      for (UIView *aView in subviews)
      {
              if ([aView isKindOfClass:[ZJProgressHUD class]])
              {
                      [huds addObject:aView];
              }
      }
     
      for (ZJProgressHUD *hud in huds)
      {
              [hud hide:animated];
      }
      return [huds count];
}

- (void)dealloc
{
     
}

static CGFloat toastWidth = 80;
- (instancetype)initWithFrame:(CGRect)frame
{
      self = [super initWithFrame:frame];
      if (self)
      {
              self.tipText = TIP_DEFULT_TEXT;
             
              _toast = [[UIView alloc] initWithFrame:CGRectMake((self.frame.size.width - toastWidth) / 2, (self.frame.size.height - toastWidth) / 2 , toastWidth, toastWidth)];
              _toast.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
              _toast.layer.cornerRadius = 10;
              _toast.hidden = YES;
              [self addSubview:_toast];
             
              _rotateView = [[UIView alloc] initWithFrame:CGRectMake((_toast.frame.size.width - 2 * SIZE_RADIUS_WIDTH) / 2, (_toast.frame.size.height - 2 * SIZE_RADIUS_WIDTH) / 2 - 5, 2 * SIZE_RADIUS_WIDTH, 2 * SIZE_RADIUS_WIDTH)];
              _rotateView.backgroundColor = [UIColor clearColor];
              [_toast addSubview:_rotateView];
             
              UIBezierPath *pathRotate= [UIBezierPath bezierPathWithArcCenter:CGPointMake(SIZE_RADIUS_WIDTH, SIZE_RADIUS_WIDTH) radius:SIZE_RADIUS_WIDTH startAngle:- M_PI_2 endAngle:(M_PI * 2) * .5 - M_PI_2 clockwise:YES];
              _rotateLayer = [CAShapeLayer layer];
              _rotateLayer.path = pathRotate.CGPath;
              _rotateLayer.fillColor = [UIColor clearColor].CGColor;
              _rotateLayer.strokeColor = [UIColor whiteColor].CGColor;
              _rotateLayer.lineWidth = 3;
              _rotateLayer.lineCap = kCALineCapRound;
              [_rotateView.layer addSublayer:_rotateLayer];
             
              CABasicAnimation *rotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
              rotateAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLi near];
              rotateAnimation.fromValue = @(2*M_PI);
              rotateAnimation.toValue = @0;
              rotateAnimation.duration = .3;
              rotateAnimation.repeatCount = HUGE;
              rotateAnimation.removedOnCompletion = NO;
              [_rotateView.layer addAnimation:rotateAnimation forKey:KEY_ANIMATION_ROTATE];
             
              _textLayer = [CAShapeLayer layer];
              _textLayer.fillColor    = [UIColor clearColor].CGColor;
              _textLayer.strokeColor = [UIColor whiteColor].CGColor;
              _textLayer.lineWidth    = 1;
              _textLayer.lineCap = kCALineCapButt;
              [_toast.layer addSublayer:_textLayer];
             
              CABasicAnimation *textAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
              textAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEa seInEaseOut];
              textAnimation.fromValue = @(0);
              textAnimation.toValue = @1;
              textAnimation.duration = 2;
              textAnimation.repeatCount = HUGE;
              textAnimation.removedOnCompletion = NO;
              [_textLayer addAnimation:textAnimation forKey:nil];
             
              self.tipText = TIP_DEFULT_TEXT;
      }
      return self;
}

- (instancetype)initWithView:(UIView *)view
{
      return [self initWithFrame:view.bounds];
}

- (instancetype)initWithWindow:(UIWindow *)window
{
      return [self initWithView:window];
}

#pragma mark SETORGET
- (void)setTipText:(NSString *)tipText
{
      _tipText = tipText;
     
      [self textLayerPath:tipText];
}

- (void)setToastColor:(UIColor *)toastColor
{
      _toastColor = toastColor;
      _toast.backgroundColor = toastColor;
}

- (void)setContentColor:(UIColor *)contentColor
{
      _rotateLayer.strokeColor = contentColor.CGColor;
      _textLayer.strokeColor = contentColor.CGColor;
}

- (void)setShowMask:(BOOL)showMask
{
      self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:showMask - .5];
}

- (void)show:(BOOL)animated
{
      self.toast.hidden = NO;
      if (animated)
      {
              self.toast.transform = CGAffineTransformScale(self.transform,0.2,0.2);
             
              [UIView animateWithDuration:.3 animations:^{
                      self.toast.transform = CGAffineTransformScale(self.transform,1.05,1.05);
              } completion:^(BOOL finished) {
                      [UIView animateWithDuration:.3 animations:^{
                              self.toast.transform = CGAffineTransformIdentit y;
                      }];
              }];
      }
}

- (void)hide:(BOOL)animated
{
      [UIView animateWithDuration:animated ? .3 : 0 delay:.1 options:UIViewAnimationOptionCur veEaseOut  animations:^{
              self.toast.transform = CGAffineTransformScale(self.transform,1.05,1.05);
      } completion:^(BOOL finished) {
              [UIView animateWithDuration:animated ? .3 : 0 animations:^{
                      self.toast.transform = CGAffineTransformScale(self.transform,0.2,0.2);
              } completion:^(BOOL finished) {
                      [self.rotateView.layer removeAnimationForKey:KEY_ANIMATION_ROTATE];
                      [self.textLayer removeAnimationForKey:KEY_ANIMATION_TEXT];
                      [self removeFromSuperview];
              }];
      }];
}

#pragma mark Methods
- (UIBezierPath *)textPath:(NSMutableAttributedStrin g *)text
{
      CGMutablePathRef letters = CGPathCreateMutable();
      CTLineRef line = CTLineCreateWithAttribut edString((__bridge CFAttributedStringRef)text);
      CFArrayRef runArray = CTLineGetGlyphRuns(line);
     
      for (CFIndex runIndex = 0; runIndex < CFArrayGetCount(runArray); runIndex++)
      {
              CTRunRef run = (CTRunRef)CFArrayGetValueAtIndex(runArray, runIndex);
              CTFontRef runFont = CFDictionaryGetValue(CTRunGetAttributes(run), kCTFontAttributeName);
             
              for (CFIndex runGlyphIndex = 0; runGlyphIndex < CTRunGetGlyphCount(run); runGlyphIndex++)
              {
                      CFRange thisGlyphRange = CFRangeMake(runGlyphIndex, 1);
                      CGGlyph glyph;
                      CGPoint position;
                      CTRunGetGlyphs(run, thisGlyphRange, &glyph);
                      CTRunGetPositions(run, thisGlyphRange, &position);
                      CGPathRef letter = CTFontCreatePathForGlyph (runFont, glyph, NULL);
                      CGAffineTransform t = CGAffineTransformMakeTra nslation(position.x, position.y);
                      CGPathAddPath(letters, &t, letter);
                      CGPathRelease(letter);
              }
      }
     
      UIBezierPath *path = [UIBezierPath bezierPathWithCGPath:letters];
      CGRect boundingBox = CGPathGetBoundingBox(letters);
      CGPathRelease(letters);
      CFRelease(line);
     
      [path applyTransform:CGAffineTransformMakeSca le(1.0, -1.0)];
      [path applyTransform:CGAffineTransformMakeTra nslation(0.0, boundingBox.size.height)];
     
      return path;
}

- (void)textLayerPath:(NSString *)text
{
      NSMutableAttributedStrin g *attributed = [[NSMutableAttributedStrin g alloc] initWithString:text];
      [attributed addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:SIZE_FONT_TIP] range:NSMakeRange(0, attributed.length)];
      _textLayer.path = [self textPath:attributed].CGPath;
     
      CGFloat width = attributed.size.width + 10;
      CGFloat maxWidth = self.frame.size.width - 100;
      if (width >= _toast.frame.size.width)
      {
              CGRect frame = _toast.frame;
              if (width <= maxWidth)
              {
                      frame.size.width = width;
              }
              else
              {
                      frame.size.width = maxWidth;
              }
              _toast.frame = frame;
      }
      else
      {
              CGRect frame = _toast.frame;
              frame.size.width = toastWidth;
              _toast.frame = frame;
      }
      _toast.center = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2);
      _rotateView.center = CGPointMake(_toast.frame.size.width / 2, _rotateView.center.y);
      _textLayer.position = CGPointMake((_toast.frame.size.width - attributed.size.width) / 2, toastWidth - 20);
}


@end
// http://blog.sina.com.cn/resoftios

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值