//
// CicleMenu.h
// Learn3
//
// Created by lance on 14-4-10.
// Copyright (c) 2014年 Lance. All rights reserved.
//
#import <UIKit/UIKit.h>
@protocol cicleMenuDelegate;
@interface CicleMenu : UIView
{
UIView *_parentView;
NSMutableArray *_itemsViewArrays;
BOOL _isAnimation;
}
@property (nonatomic, strong) NSArray *items; // image name
@property (nonatomic, assign) float cornerRadius; // 圆角,
@property (nonatomic, assign) float radius; // 页面半径
@property (nonatomic, assign) float itemHeight; // item 的变长,正方形
@property (nonatomic, strong) NSArray *itemColors; // item 颜色 未设定,默认是blue
@property (nonatomic, strong) UIView *backgroundView; // item 所在背景,遮罩层
@property (nonatomic, strong) void (^completion)(); // 动画完成之后相应
@property (nonatomic, assign) id<cicleMenuDelegate> delegate;
- (id)initWithRadius:(float)radius cornerRadisu:(float)cornerRadius itemHeight:(float)itemHeight parentView:(UIView *)parent;
- (void)show;
- (void)hide:(NSInteger)index;
@end
@protocol cicleMenuDelegate <NSObject>
- (void)cicleMenu:(CicleMenu *)cicleMenu onClickWithIndex:(NSInteger)index;
@end
//
// CicleMenu.m
// Learn3
//
// Created by lance on 14-4-10.
// Copyright (c) 2014年 Lance. All rights reserved.
//
#import "CicleMenu.h"
@implementation CicleMenu
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (id)initWithRadius:(float)radius cornerRadisu:(float)cornerRadius itemHeight:(float)itemHeight parentView:(UIView *)parent
{
if (parent == nil) {
@throw @"parent is nil";
return nil;
}
CGPoint centerPoint = parent.center;
CGRect frame = CGRectMake(centerPoint.x - radius, centerPoint.y - radius, radius * 2, radius * 2);
self = [super initWithFrame:frame];
if (self) {
_radius = radius;
_cornerRadius = cornerRadius;
_itemHeight = itemHeight;
_parentView = parent;
_backgroundView = [[UIView alloc] initWithFrame:parent.frame];
_backgroundView.backgroundColor = [UIColor whiteColor];
_backgroundView.alpha = 0.3;
[_parentView addSubview:_backgroundView];
}
return self;
}
- (void)show
{
if (_isAnimation) {
return;
}
if (_itemsViewArrays) {
_itemsViewArrays = nil;
}
_itemsViewArrays = [NSMutableArray array];
if (_itemColors == nil) {
_itemColors = @[[UIColor blueColor], [UIColor blueColor], [UIColor blueColor], [UIColor blueColor], [UIColor blueColor]];
}
_isAnimation = YES;
CGPoint center = _backgroundView.center;
float angle = 0.0;
float delaytime = 0.0;
float x = 0.0;
float y = 0.0;
float space = 360.0 / [_items count];
for (int i = 0; i < [_items count]; i ++) {
delaytime = i * 0.35;
angle = space * i;
// 计算item的起始坐标
x = center.x + _radius * sin(angle * M_PI / 180.0) - _itemHeight * 0.5;
y = center.y + _radius * cos(angle * M_PI / 180.0) - _itemHeight * 0.5;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:@selector(buttonOnClick:) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(x, y, _itemHeight, _itemHeight);
button.alpha = 0.0;
button.tag = i;
UIView *cicleView = [[UIView alloc] initWithFrame:button.frame];
cicleView.layer.cornerRadius = _cornerRadius;
cicleView.layer.masksToBounds = YES;
cicleView.opaque = NO;
cicleView.backgroundColor = [_itemColors objectAtIndex:i];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[_items objectAtIndex:i]]];
CGRect frame = imageView.frame;
frame.origin.x = (CGRectGetWidth(cicleView.frame) - CGRectGetWidth(frame)) * 0.5;
frame.origin.y = (CGRectGetHeight(cicleView.frame) - CGRectGetHeight(frame)) * 0.5;
imageView.frame = frame;
[cicleView addSubview:imageView];
[button setBackgroundImage:[self getImageFromView:cicleView] forState:UIControlStateNormal];
[_backgroundView addSubview:button];
[_itemsViewArrays addObject:button];
[self performSelector:@selector(animationShow:) withObject:button afterDelay:delaytime];
}
}
- (void)animationShow:(UIButton *)button
{
[UIView animateWithDuration:0.15 animations:^{
button.alpha = 1.0;
button.transform = CGAffineTransformMakeScale(1.2, 1.2);
} completion:^(BOOL finished) {
if (finished) {
[UIView animateWithDuration:0.10 animations:^{
button.transform = CGAffineTransformMakeScale(0.7, 0.7);
} completion:^(BOOL finished) {
if (finished) {
[UIView animateWithDuration:0.10 animations:^{
button.transform = CGAffineTransformMakeScale(1.0, 1.0);
} completion:^(BOOL finished) {
if (finished) {
if (button.tag == [_items count] - 1) {
_isAnimation = NO;
}
}
}];
}
}];
}
}];
}
- (void)animationHide:(UIButton *)button
{
[UIView animateWithDuration:0.15 animations:^{
button.transform = CGAffineTransformMakeScale(1.2, 1.2);
} completion:^(BOOL finished) {
if (finished) {
[UIView animateWithDuration:0.10 animations:^{
button.transform = CGAffineTransformMakeScale(0.7, 0.7);
} completion:^(BOOL finished) {
if (finished) {
[UIView animateWithDuration:0.10 animations:^{
button.transform = CGAffineTransformMakeScale(1.0, 1.0);
button.alpha = 0.0;
} completion:^(BOOL finished) {
if (finished) {
[button removeFromSuperview];
[_itemsViewArrays removeObject:button];
if ([_itemsViewArrays count] == 0) {
[_backgroundView removeFromSuperview];
_isAnimation = NO;
self.completion();
}
}
}];
}
}];
}
}];
}
- (void)hide:(NSInteger)index
{
if (_isAnimation) {
return;
}
_isAnimation = YES;
float delaytime = 0;
int j = 0;
for (int i = index; i < [_items count] + index; i ++) {
delaytime = j * 0.35;
[self performSelector:@selector(animationHide:) withObject:[_itemsViewArrays objectAtIndex:(i % [_items count])] afterDelay:delaytime];
j ++;
}
}
- (void)buttonOnClick:(UIButton *)button
{
if (_isAnimation) {
return;
}
if ([self.delegate respondsToSelector:@selector(cicleMenu:onClickWithIndex:)]) {
[self.delegate cicleMenu:self onClickWithIndex:button.tag];
}
}
- (UIImage *)getImageFromView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
@end
调用
//
// TestViewController.m
// Learn3
//
// Created by lance on 14-4-10.
// Copyright (c) 2014年 Lance. All rights reserved.
//
#import "TestViewController.h"
#import "CicleMenu.h"
@interface TestViewController () <cicleMenuDelegate>
{
CicleMenu *_cicleMenu;
UIButton *_button;
}
@end
@implementation TestViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)loadView
{
[super loadView];
self.view.backgroundColor = [UIColor grayColor];
_button = [UIButton buttonWithType:UIButtonTypeContactAdd];
_button.frame = CGRectMake(100, 100, 70, 70);
[_button addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_button];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)onClick:(UIButton *)button
{
button.hidden = YES;
_cicleMenu = [[CicleMenu alloc] initWithRadius:120.0 cornerRadisu:30.0 itemHeight:80.0 parentView:self.view];
_cicleMenu.items = @[@"icon-aa-at", @"icon-aa-facebook", @"icon-aa-googleplus", @"icon-aa-twitter", @"icon-aa-tumblr"];
_cicleMenu.itemColors = @[[UIColor redColor], [UIColor blueColor], [UIColor greenColor], [UIColor orangeColor], [UIColor purpleColor]];
_cicleMenu.delegate = self;
[_cicleMenu show];
}
- (void)cicleMenu:(CicleMenu *)cicleMenu onClickWithIndex:(NSInteger)index
{
[cicleMenu hide:index];
__block UIButton *button = _button;
_cicleMenu.completion = ^() {
button.hidden = NO;
};
}
@end
菜单,圆周菜单 动画效果
猜你喜欢
转载自blog.csdn.net/lanliang901125/article/details/23373821
今日推荐
周排行