[控件] 心形加载的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

目录
相关文章
|
29天前
|
小程序 容器
小程序图片水平垂直居中显示在view中
小程序图片水平垂直居中显示在view中
|
Windows
Winform控件优化之背景透明那些事1:Button控件等背景透明
WinForm不支持真正的透明,其控件透明的实现都是背景颜色设置和对应位置的父控件背景相同。 Winform中控件的背景透明只有三种:Button控件的透明、其他控件的透明...
2497 0
Winform控件优化之背景透明那些事1:Button控件等背景透明
|
9月前
WPF-样式问题-ListBox或ListView中子项全填充去除边线问题
WPF-样式问题-ListBox或ListView中子项全填充去除边线问题
73 0
|
11月前
|
XML Android开发 数据格式
控件布局(View)叠加效果
控件布局(View)叠加效果
|
Android开发
自定义 View | 画板
自定义 View | 画板
自定义 View | 画板
|
XML Android开发 数据格式
Android控件显示、隐藏时,增加动画效果
Android控件显示、隐藏时,增加动画效果
452 0
Android控件显示、隐藏时,增加动画效果
如何在 C#中的listView 控件中显示图片?
如何在 C#中的listView 控件中显示图片?
990 0
如何在 C#中的listView 控件中显示图片?
|
开发工具 C语言
Qt编写自定义控件33-图片切换动画
一、前言 在很多看图软件中,切换图片的时候可以带上动画过渡或者切换效果,显得更人性化,其实主要还是炫一些,比如百叶窗、透明度变化、左下角飞入等,无论多少种效果,核心都是围绕QPainter来进行,将各种动画效果对应的图片的区域动态计算并绘制出来,配合以QPropertyAnimation动画属性产生线性插值,比如渐入飞入时候,可以中间快速两端慢速。
945 0
|
Android开发 iOS开发 索引
Xamarin自定义布局系列——支持无限滚动的自动轮播视图CarouselView
原文:Xamarin自定义布局系列——支持无限滚动的自动轮播视图CarouselView 背景简述 自动轮播视图(CarouselView)在现在App中的地位不言而喻,绝大多数的App中都有类似的视图,无论是WebApp还是Native App。
1833 0
|
前端开发 API Android开发
3.3 自定义控件基础 之 View的绘制
当测量好了一个View之后,我们就可以简单地重写onDraw()方法,并在Canvas对象上来绘制所需要的图形。首先我们来了解一下利用系统2D绘图API所必须要使用到的Canvas对象。
659 0