原创帖子,转载请注明出处:http://blog.csdn.net/sbvfhp/article/details/48111055
自动移动效果图:
核心代码如下:
//
// EFAnimationViewController.m
// aaatest
//
// Created by 李建 on 15/5/17.
// Copyright (c) 2015年 李建. All rights reserved.
//
#import "EFAnimationViewController.h"
#define RADIUS 100.0
#define PHOTONUM 3
#define TAGSTART 1000
#define TIME 1.5
#define SCALENUMBER 1.25
NSInteger array [PHOTONUM][PHOTONUM] = {
{0,1,2},
{2,0,1},
{1,2,0}
// {0,1,2,3,4},
// {4,0,1,2,3},
// {3,4,0,1,2}
// {2,3,4,0,1},
// {1,2,3,4,0}
};
@interface EFAnimationViewController ()<EFItemViewDelegate>
@property (nonatomic, assign) NSInteger currentTag;
@end
@implementation EFAnimationViewController
CATransform3D rotationTransform1[PHOTONUM];
- (void)viewDidLoad {
[super viewDidLoad];
[self configViews];
[self performSelector:@selector(temp:) withObject:@"1001" afterDelay:1];
[self performSelector:@selector(temp:) withObject:@"1002" afterDelay:3];
}
-(void)temp:(NSString *)tag{
if ([@"1001" isEqualToString:tag]) {
[self didTapped:1001];
}else if ([@"1002" isEqualToString:tag]){
[self didTapped:1002];
}
}
#pragma mark - configViews
- (void)configViews {
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"common_background"]];
NSArray *dataArray = @[@"exer_icon_biology", @"exer_icon_chemistry", @"exer_icon_chinese", @"exer_icon_english", @"exer_icon_geography"];
CGFloat centery = self.view.center.y - 50;
CGFloat centerx = self.view.center.x;
for (NSInteger i = 0;i < PHOTONUM;i++) {
CGFloat tmpy = centery;
CGFloat tmpx = centerx + i * 100;
EFItemView *view = [[EFItemView alloc] initWithNormalImage:dataArray[i] highlightedImage:[dataArray[i] stringByAppendingFormat:@"%@", @"_hover"] tag:TAGSTART+i title:nil];
view.frame = CGRectMake(0.0, 0.0,100,100);
view.center = CGPointMake(tmpx,tmpy);
view.delegate = self;
rotationTransform1[i] = CATransform3DIdentity;
CGFloat Scalenumber = fabs(i - PHOTONUM/2.0)/(PHOTONUM/2.0);
if (Scalenumber < 0.3) {
Scalenumber = 0.4;
}
CATransform3D rotationTransform = CATransform3DIdentity;
rotationTransform = CATransform3DScale (rotationTransform, Scalenumber*SCALENUMBER,Scalenumber*SCALENUMBER, 1);
view.layer.transform=rotationTransform;
[self.view addSubview:view];
}
self.currentTag = TAGSTART;
}
#pragma mark - EFItemViewDelegate
- (void)didTapped:(NSInteger)index {
if (self.currentTag == index) {
NSLog(@"自定义处理事件");
return;
}
NSInteger t = [self getItemViewTag:index];
for (NSInteger i = 0;i<PHOTONUM;i++ ) {
UIView *view = [self.view viewWithTag:TAGSTART+i];
[view.layer addAnimation:[self moveanimation:TAGSTART+i number:t] forKey:@"position"];
[view.layer addAnimation:[self setscale:TAGSTART+i clicktag:index] forKey:@"transform"];
}
self.currentTag = index;
}
- (CAAnimation*)setscale:(NSInteger)tag clicktag:(NSInteger)clicktag {
NSInteger i = array[clicktag - TAGSTART][tag - TAGSTART];
NSInteger i1 = array[self.currentTag - TAGSTART][tag - TAGSTART];
CGFloat Scalenumber = fabs(i - PHOTONUM/2.0)/(PHOTONUM/2.0);
CGFloat Scalenumber1 = fabs(i1 - PHOTONUM/2.0)/(PHOTONUM/2.0);
if (Scalenumber < 0.3) {
Scalenumber = 0.4;
}
CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform"];
animation.duration = TIME;
animation.repeatCount =1;
CATransform3D dtmp = CATransform3DScale(rotationTransform1[tag - TAGSTART],Scalenumber*SCALENUMBER, Scalenumber*SCALENUMBER, 1.0);
animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DScale(rotationTransform1[tag - TAGSTART],Scalenumber1*SCALENUMBER,Scalenumber1*SCALENUMBER, 1.0)];
animation.toValue = [NSValue valueWithCATransform3D:dtmp ];
animation.autoreverses = NO;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
return animation;
}
- (CAAnimation*)moveanimation:(NSInteger)tag number:(NSInteger)num {
// CALayer
UIView *view = [self.view viewWithTag:tag];
CAKeyframeAnimation* animation;
animation = [CAKeyframeAnimation animation];
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL,view.layer.position.x,view.layer.position.y);
NSInteger p = [self getItemViewTag:tag];
CGFloat centery = self.view.center.y - 50;
CGFloat centerx = self.view.center.x - 80 * num;
CGFloat tmpy = centery;
CGFloat tmpx = centerx + p * 100;
view.center = CGPointMake(tmpx,tmpy);
CGPathAddLineToPoint(path, nil, tmpx, tmpy);
animation.path = path;
CGPathRelease(path);
animation.duration = TIME;
animation.repeatCount = 1;
animation.calculationMode = @"paced";
return animation;
}
- (NSInteger)getItemViewTag:(NSInteger)tag {
return tag - TAGSTART;
}
@end
@interface EFItemView ()
@property (nonatomic, strong) NSString *normal;
@property (nonatomic, strong) NSString *highlighted_;
@property (nonatomic, assign) NSInteger tag_;
@property (nonatomic, strong) NSString *title;
@end
@implementation EFItemView
- (instancetype)initWithNormalImage:(NSString *)normal highlightedImage:(NSString *)highlighted tag:(NSInteger)tag title:(NSString *)title {
self = [super init];
if (self) {
_normal = normal;
_highlighted_ = highlighted;
_tag_ = tag;
_title = title;
[self configViews];
}
return self;
}
#pragma mark - configViews
- (void)configViews {
self.tag = _tag_;
[self setBackgroundImage:[UIImage imageNamed:_normal] forState:UIControlStateNormal];
[self setBackgroundImage:[UIImage imageNamed:_highlighted_] forState:UIControlStateHighlighted];
[self setTitle:_title forState:UIControlStateNormal];
[self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.titleLabel setFont:[UIFont systemFontOfSize:30.0]];
}
@end