[控件] 心形加载的view

简介:

心形加载的view

效果:

素材图片:

源码:

StarView.h 与 StarView.m

//
//  StarView.h
//  Star
//
//  Created by XianMingYou on 15/3/13.
//  Copyright (c) 2015年 XianMingYou. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface StarView : UIView


@property (nonatomic, strong) UIColor        *backgroundViewColor;
@property (nonatomic, strong) UIColor        *animationViewColor;
@property (nonatomic, assign) NSTimeInterval  animationDuration;

+ (instancetype)createWithFrame:(CGRect)frame
            backgroundViewColor:(UIColor *)bgColor
             animationViewColor:(UIColor *)anColor;

- (void)percent:(CGFloat)percent animated:(BOOL)animated;

@end


//
//  StarView.m
//  Star
//
//  Created by XianMingYou on 15/3/13.
//  Copyright (c) 2015年 XianMingYou. All rights reserved.
//

#import "StarView.h"
#import "UIView+SetRect.h"

@interface StarView ()

@property (nonatomic, strong) UIImageView  *imageView;      // 图片
@property (nonatomic, strong) UIView       *backgroundView; // 背景色View
@property (nonatomic, strong) UIView       *animationView;  // 做动画的View

@property (nonatomic) CGFloat height;
@property (nonatomic) CGFloat width;

@end

@implementation StarView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.layer.masksToBounds = YES;
        self.height              = frame.size.height;
        self.width               = frame.size.width;
        
        [self addSubview:self.backgroundView];
        
        [self addSubview:self.animationView];
        
        [self initImageView];
    }
    return self;
}

- (void)initImageView {
    self.imageView       = [[UIImageView alloc] initWithFrame:self.bounds];
    self.imageView.image = [UIImage imageNamed:@"star"];
    
    [self addSubview:self.imageView];
}

- (void)percent:(CGFloat)percent animated:(BOOL)animated {
    // 过滤percent
    if (percent <= 0) {
        percent = 0;
    } else if (percent >= 1) {
        percent = 1;
    }

    if (animated == NO) {
        CGFloat positionY = self.height * (1 - percent);
        _animationView.y  = positionY;
    } else {
        CGFloat positionY = self.height * (1 - percent);
        [UIView animateWithDuration:(self.animationDuration <= 0 ? 0.5 : self.animationDuration)
                         animations:^{
            _animationView.y = positionY;
        }];
    }
}

+ (instancetype)createWithFrame:(CGRect)frame
            backgroundViewColor:(UIColor *)bgColor
             animationViewColor:(UIColor *)anColor {
    StarView *star           = [[StarView alloc] initWithFrame:frame];
    star.backgroundViewColor = bgColor;
    star.animationViewColor  = anColor;

    return star;
}

@synthesize backgroundView = _backgroundView;
- (UIView *)backgroundView {
    if (_backgroundView == nil) {
        _backgroundView = [[UIView alloc] initWithFrame:self.bounds];
    }
    
    return _backgroundView;
}

@synthesize animationView = _animationView;
- (UIView *)animationView {
    if (_animationView == nil) {
        _animationView = [[UIView alloc] initWithFrame:CGRectMake(0, self.height, self.width, self.height)];
    }
    
    return _animationView;
}

@synthesize backgroundViewColor = _backgroundViewColor;
- (UIColor *)backgroundViewColor {
    return _backgroundViewColor;
}
- (void)setBackgroundViewColor:(UIColor *)backgroundViewColor {
    _backgroundViewColor            = backgroundViewColor;
    _backgroundView.backgroundColor = backgroundViewColor;
}

@synthesize animationViewColor = _animationViewColor;
- (UIColor *)animationViewColor {
    return _animationViewColor;
}
- (void)setAnimationViewColor:(UIColor *)animationViewColor {
    _animationViewColor            = animationViewColor;
    _animationView.backgroundColor = animationViewColor;
}
@end

辅助文件  UIView+SetRect.h 与  UIView+SetRect.m
//
//  UIView+SetRect.h
//  TestPch
//
//  Created by YouXianMing on 14-12-26.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface UIView (SetRect)

// Frame
@property (nonatomic) CGPoint viewOrigin;
@property (nonatomic) CGSize  viewSize;

// Frame Origin
@property (nonatomic) CGFloat x;
@property (nonatomic) CGFloat y;

// Frame Size
@property (nonatomic) CGFloat width;
@property (nonatomic) CGFloat height;

// Frame Borders
@property (nonatomic) CGFloat top;
@property (nonatomic) CGFloat left;
@property (nonatomic) CGFloat bottom;
@property (nonatomic) CGFloat right;

// Center Point
#if !IS_IOS_DEVICE
@property (nonatomic) CGPoint center;
#endif
@property (nonatomic) CGFloat centerX;
@property (nonatomic) CGFloat centerY;

// Middle Point
@property (nonatomic, readonly) CGPoint middlePoint;
@property (nonatomic, readonly) CGFloat middleX;
@property (nonatomic, readonly) CGFloat middleY;
@property (nonatomic, assign) CGFloat cornerRadius ;
@property (nonatomic ,assign) BOOL round ;
@end


//
//  UIView+SetRect.m
//  TestPch
//
//  Created by YouXianMing on 14-12-26.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import "UIView+SetRect.h"

@implementation UIView (SetRect)

#pragma mark Frame

- (CGPoint)viewOrigin
{
    return self.frame.origin;
}

- (void)setViewOrigin:(CGPoint)newOrigin
{
    CGRect newFrame = self.frame;
    newFrame.origin = newOrigin;
    self.frame = newFrame;
}

- (CGSize)viewSize
{
    return self.frame.size;
}

- (void)setViewSize:(CGSize)newSize
{
    CGRect newFrame = self.frame;
    newFrame.size = newSize;
    self.frame = newFrame;
}


#pragma mark Frame Origin

- (CGFloat)x
{
    return self.frame.origin.x;
}

- (void)setX:(CGFloat)newX
{
    CGRect newFrame = self.frame;
    newFrame.origin.x = newX;
    self.frame = newFrame;
}

- (CGFloat)y
{
    return self.frame.origin.y;
}

- (void)setY:(CGFloat)newY
{
    CGRect newFrame = self.frame;
    newFrame.origin.y = newY;
    self.frame = newFrame;
}


#pragma mark Frame Size

- (CGFloat)height
{
    return self.frame.size.height;
}

- (void)setHeight:(CGFloat)newHeight
{
    CGRect newFrame = self.frame;
    newFrame.size.height = newHeight;
    self.frame = newFrame;
}

- (CGFloat)width
{
    return self.frame.size.width;
}

- (void)setWidth:(CGFloat)newWidth
{
    CGRect newFrame = self.frame;
    newFrame.size.width = newWidth;
    self.frame = newFrame;
}


#pragma mark Frame Borders

- (CGFloat)left
{
    return self.x;
}

- (void)setLeft:(CGFloat)left
{
    self.x = left;
}

- (CGFloat)right
{
    return self.frame.origin.x + self.frame.size.width;
}

- (void)setRight:(CGFloat)right
{
    self.x = right - self.width;
}

- (CGFloat)top
{
    return self.y;
}

- (void)setTop:(CGFloat)top
{
    self.y = top;
}

- (CGFloat)bottom
{
    return self.frame.origin.y + self.frame.size.height;
}

- (void)setBottom:(CGFloat)bottom
{
    self.y = bottom - self.height;
}


#pragma mark Center Point

#if !IS_IOS_DEVICE
- (CGPoint)center
{
    return CGPointMake(self.left + self.middleX, self.top + self.middleY);
}

- (void)setCenter:(CGPoint)newCenter
{
    self.left = newCenter.x - self.middleX;
    self.top = newCenter.y - self.middleY;
}
#endif

- (CGFloat)centerX
{
    return self.center.x;
}

- (void)setCenterX:(CGFloat)newCenterX
{
    self.center = CGPointMake(newCenterX, self.center.y);
}

- (CGFloat)centerY
{
    return self.center.y;
}

- (void)setCenterY:(CGFloat)newCenterY
{
    self.center = CGPointMake(self.center.x, newCenterY);
}


#pragma mark Middle Point

- (CGPoint)middlePoint
{
    return CGPointMake(self.middleX, self.middleY);
}

- (CGFloat)middleX
{
    return self.width / 2;
}

- (CGFloat)middleY
{
    return self.height / 2;
}

- (void)setCornerRadius:(CGFloat)cornerRadius
{
    self.layer.masksToBounds = YES ;
    self.layer.cornerRadius = cornerRadius ;
}

- (void)setRound:(BOOL)round
{
    [self setCornerRadius:self.height/2];
}

- (CGFloat)cornerRadius
{
    return  self.layer.cornerRadius ;
}

- (BOOL)round
{
    return NO ;
}

@end

使用时候的源码:
//
//  ViewController.m
//  Star
//
//  Created by XianMingYou on 15/3/13.
//  Copyright (c) 2015年 XianMingYou. All rights reserved.
//

#import "ViewController.h"
#import "StarView.h"

@interface ViewController ()

@property (nonatomic, strong) StarView  *star;
@property (nonatomic, strong) NSTimer   *timer;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.star = [StarView createWithFrame:CGRectMake(0, 0, 100, 100)
                      backgroundViewColor:[[UIColor redColor] colorWithAlphaComponent:0.05f]
                       animationViewColor:[[UIColor redColor] colorWithAlphaComponent:0.5f]];
    self.star.animationDuration   = 1.f;
    self.star.center              = self.view.center;
    [self.view addSubview:self.star];
    
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.5f
                                                  target:self
                                                selector:@selector(timerEvent:)
                                                userInfo:nil
                                                 repeats:YES];
}

- (void)timerEvent:(id)sender {
    CGFloat percent = arc4random() % 100 / 100.f;
    [self.star percent:percent animated:YES];
}

@end

目录
相关文章
|
消息中间件 JSON Java
RabbitTemplate 发送接受消息& amp 序列化机制|学习笔记
快速学习 RabbitTemplate 发送接受消息& amp 序列化机制
518 0
RabbitTemplate 发送接受消息& amp 序列化机制|学习笔记
|
SQL XML Java
第15篇:Mybatis中打印Sql信息
在Mybatis中如果我们要对我们的sql信息进行检查, 只能启动Spring容器, 去执行根据成功和失败来判断我们的逻辑是否有问题。 此时会比较耗时,因为要启动容器。基于这个痛点, 本文要设计一个工具。使我们不依赖Spring容器,也不依赖任何外部插件,直接就把 Sql信息的打印出来。
373 0
|
存储 数据库 uml
UML里边界类、控制类和实体类三类讲解(2011年系统分析师考点分析之一)
UML里边界类、控制类和实体类三类讲解 2011年系统分析师考点分析之一   一.UML将类分为哪三个类: 1.边界类(Boundry Class); 2.实体类(Entity Class); 3.控制类(Control Class); 二.边界类 1.用来描述什么问题? 边界类用于描述外部参与者与系统之间的交互。
2722 0
|
存储 NoSQL 关系型数据库
重构之道:揭秘大规模系统重构的经验与挑战
重构之道:揭秘大规模系统重构的经验与挑战
1381 2
|
数据采集 领域建模 数据库
如何画领域模型图(数据架构/ER图)
如何画领域模型图(数据架构/ER图)
4157 1
如何画领域模型图(数据架构/ER图)
|
Linux Go Windows
go windows编译linux可执行文件
go windows编译linux可执行文件
8486 0
|
定位技术
阿里架构总监一次讲透中台架构,13页PPT精华详解,建议收藏!
本文整理了阿里几位技术专家,如架构总监 谢纯良,中间件技术专家 玄难等几位大牛,关于中台架构的几次分享内容,将业务中台形态、中台全局架构、业务中台化、中台架构图、中台建设方法论、中台组织架构、企业中台建设实施步骤等总共13页PPT精华的浓缩,供大家学习借鉴。
35375 15
|
存储 数据采集 安全
各种系统架构图与详细说明
原文:各种系统架构图与详细说明 共享平台逻辑架构设计 如上图所示为本次共享资源平台逻辑架构图,上图整体展现说明包括以下几个方面: 1 应用系统建设 本次项目的一项重点就是实现原有应用系统的全面升级以及新的应用系统的开发,从而建立行业的全面的应用系统架构群。
25052 1
|
Cloud Native 架构师 测试技术
如何画好一张架构图?(内含知识图谱)
架构图是什么?为什么要画架构图?如何画好架构图?有哪些方法?本文从架构的定义说起,分享了阿里文娱高级技术专家箫逸关于画架构图多年的经验总结,并对抽象这一概念进行了深入地讨论。内容较长,同学们可收藏起来细细阅读。
13837 0
如何画好一张架构图?(内含知识图谱)