码迷,mamicode.com
首页 > 其他好文 > 详细

[控件] 心形加载的view

时间:2015-03-13 22:12:35      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:

心形加载的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

 

[控件] 心形加载的view

标签:

原文地址:http://www.cnblogs.com/YouXianMing/p/4335983.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!