[控件] 心形加载的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
时间: 2024-12-27 16:53:51

[控件] 心形加载的view的相关文章

Webbrowser控件判断网页加载完毕的简单方法 (转)

摘自:http://blog.csdn.net/cometnet/article/details/5261192 一般情况下,当ReadyState属性变成READYSTATE_COMPLETE时,Webbrowser控件会通过触发DocumentCompleted事件来指示网页加载完毕.但当加载的网页包含frame时,可能会多次触发该事件,所以不能简单地通过它来判断网页加载完毕. 从微软的官方网站上了解到,并非每个frame都对应了一个DocumentCompleted事件,只有触发了Down

Webbrowser控件判断网页加载完毕的简单方法

一般情况下,当ReadyState属性变成READYSTATE_COMPLETE时,Webbrowser控件会通过触发DocumentCompleted事件来指示网页加载完毕.但当加载的网页包含frame时,可能会多次触发该事件,所以不能简单地通过它来判断网页加载完毕.从微软的官方网站上了解到,并非每个frame都对应了一个DocumentCompleted事件,只有触发了DownloadBegin事件的frame才会有相应的DocumentCompleted事件.另外,最外层的frame总是最

使用SplashScreenManager控件定制程序加载页面

需要devexpress版本在12.0及以上才支持 https://www.cnblogs.com/wuhuacong/p/6112461.html 在DevExpress程序中使用SplashScreenManager控件实现启动闪屏和等待信息窗口 http://blog.csdn.net/archielau/article/details/37401443 ProgressPanel.WaitForm.SplashScreenManager http://blog.csdn.net/qq99

Delphi安装DevExpress控件后,加载图片自动调用dxGDIPlusClasses的问题

Delphi安装DevExpress控件后, image控件用 Picture.LoadFromFile()  加载图片,会自动调用dxGDIPlusClasses,可以正常显示图片. FastReport中的image控件 Picture.LoadFromFile()  加载图片,也会自动调用dxGDIPlusClasses,这可能导致image控件中的图片无法显示. 解决办法是: 修改控件源码: dxGDIPlusClasses 单元中,注释掉一行代码: {$DEFINE DXREGISTE

IOS 开发笔记-基础 UI(6)照片浏览器(控件的懒加载)

使用UIImageView.UILabel.UIButton实现一个综合小案例 功能分析 (1)点击箭头切换序号.图片.描述 (2)如果是首张图片,左边箭头不能点击 (3)如果是尾张图片,右边箭头不能点击 步骤分析 (1)搭建UI界面 (2)监听按钮点击 切换序号.图片.描述 1. 界面分析 1> 需要读取或修改的属性的控件 // 序号标签 // 图片 // 图片描述 // 左边按钮 // 右边按钮 2> 需要监听响应事件的对象,需要添加监听方法 // 左边按钮 // 右边按钮 uiimage

zTree 树形控件 ajax动态加载数据

很久没搞过树形控件了 , 再次接触看官网文档有点没懂,于是在网上找了个代码copy上,但数据是写死的,就想这在用ajax异步取出数据替换,下面是js代码 <SCRIPT type="text/javascript" > //定义全局ztree数据 var zNodes; /* 初始化ztree数据 */ function initZtree(){ $.ajax({ type: "GET", url: "<%=request.getCont

jQuery EasyUI动态添加控件或者ajax加载页面后不能自动渲染问题的解决方法

转自:http://www.cnblogs.com/sunjie9606/archive/2012/09/13/2683636.html 现象: AJAX返回的html无法做到自动渲染为EasyUI的样式.比如:class="easyui-layout" 等 处理方法: 在html片段加载完毕后使用 Js代码 $.parser.parse(context) 即可重新渲染. 实现原理: 首先附上jquery.parser.js的源码 Js代码 (function($){ $.parser

界面存在多个easyUI Combobox控件时,加载数据失败,或加载不正确

问题原因:当一个界面上有多个easyui ComboBox控件,且在界面加载时同时请求数据.由于数据是异步加载,会导致下拉数据源加载失败,或加载不正确(串数据). 方案一:不用在一开始设定url(data加载路径),在点击控价下拉按钮时再加载数据源. <td>处理类型:</td> <td><input id="handleType" name="handleType" panelHeight="auto"

【iOS开发】添加子控件方式(懒加载,GCC)

1 // 2 // ViewController.m 3 // GCC 4 // 5 // Created by admin on 15/10/7. 6 // Copyright © 2015年 admin. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 11 @interface ViewController () 12 13 @property (nonatomic, strong) UIButton* bt