旋转动画

来源:互联网 发布:js传值到jsp 编辑:程序博客网 时间:2024/04/30 10:59

整理了一下旋转动画,无限旋转90度的发现当初遇到的一些问题

- (void)HighlightAnimation{

    __block typeof(self) Self =self;

    [UIViewanimateWithDuration:1.5f

animations:^{

        Self.myImageView.transform =CGAffineTransformRotate(Self.myImageView.transform, -M_PI_2);

    } 

completion:^(BOOL finished) {

        [Self DarkAnimation];

    }];

}

- (void)DarkAnimation{

    __block typeof(self) Self =self;

    [UIViewanimateWithDuration:1.5f

animations:^{

        Self.myImageView.transform =CGAffineTransformRotate(Self.myImageView.transform,M_PI_2);

    } 

completion:^(BOOL finished) {

        [Self HighlightAnimation];

    }];

}

在viewdidload里面调用就会OK

还有一种是如下类型,方法一的动画不是在块里面执行的与上面的有差别啊
在ViewDidLoad里面调用下面的方法即可
//方法1
#pragma mark动画先顺时针旋转90度再逆时针旋转90
- (void)setAnimated{
   
UIImage *image = [UIImageimageNamed:@"2"];
   
self.myImageView= [[UIImageViewalloc]initWithImage:image];
    [
self.myImageViewsetFrame:CGRectMake(50,120,100,100)];
   
self.view.backgroundColor= [UIColorwhiteColor];
    [
self.viewaddSubview:self.myImageView];
 
    [
UIViewanimateWithDuration:0
delay:0
options:UIViewAnimationOptionRepeat
animations:^{
        [
UIViewsetAnimationDuration:1.5f];
       
self.myImageView.transform=CGAffineTransformIdentity;
       
self.myImageView.transform=CGAffineTransformRotate(self.myImageView.transform,M_PI_2);
    }
completion:^(BOOLfinished) {
       
        [
self.myImageViewremoveFromSuperview];
        [
UIViewanimateWithDuration:0
delay:0
options:UIViewAnimationOptionRepeat
animations:^{
            [
UIViewsetAnimationDuration:1.5f];
           
self.myImageView.transform=CGAffineTransformRotate(self.myImageView.transform, -M_PI_2);
           
        }
completion:^(BOOLfinished) {
        }];
       
    }];
}
//方法2
#pragma mark动画先顺时针旋转90度再逆时针旋转90
/*
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //
离屏后会remove animation,这里重新添加
    [self startAnimatew];
    //
程序从后台进入激活状态需要重新添加Animation
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startAnimatew) name:@"kAPPEnterForeground" object:nil];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"kAPPEnterForeground" object:nil];
}
*/

- (
UIImageView*)createImageViewWithFrame:(CGRect)frame tag:(NSInteger)tag named:(NSString*)name{
   
UIImageView *imageView = [[UIImageViewalloc]initWithFrame:frame];
    imageView.
tag= tag;
    imageView.
image= [UIImageimageNamed:name];
   
return imageView;
}
- (
void)createSubView{
   
_firstView = [[UIViewalloc]initWithFrame:CGRectMake(60,40,60,60)];
    [
self.viewaddSubview:_firstView];
   
CGRect frame = self.firstView.bounds;
   
UIImageView *imageView = [selfcreateImageViewWithFrame:frametag:kTAG_BASE_VALUEnamed:@"2.jpg"];
   
//定位点,不写默认是中心点
//    imageView.layer.anchorPoint = CGPointMake(28/45, 16/45);

    imageView.
frame= frame;
    [
self.firstViewaddSubview:imageView];

}
- (
void)startAnimatew{
   
id fromValue = [NSNumbernumberWithFloat:-M_PI_4];
   
id toValue = [NSNumbernumberWithFloat:M_PI_4];
   
UIImageView *imageView = [self.firstViewviewWithTag:kTAG_BASE_VALUE];
    [
selfanimationWithView:imageViewkeyPath:@"transform.rotation.z"fromValue:fromValuetoValue:toValue];

}

- (
void)animationWithView:(UIView*)view keyPath:(NSString*)keyPath fromValue:(id)fromValue toValue:(id)toValue{
   
CAAnimation *animation = [selfcreateAnimationWithKeyPath:keyPathfromValue:fromValuetoValue:toValue];
    [view.
layeraddAnimation:animationforKey:nil];
}
- (
CAAnimation*)createAnimationWithKeyPath:(NSString*)keyPath fromValue:(id)fromValue toValue:(id)toValue{
   
CABasicAnimation *animation = [CABasicAnimationanimationWithKeyPath:keyPath];
    animation.
duration=1.5;//持续时间
   
CAMediaTimingFunction *mediaTiming = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.
timingFunction= mediaTiming;
    animation.
repeatCount=HUGE_VALF;//重复次数
    animation.
fromValue=  fromValue;//起始角度
    animation.
toValue= toValue;//终止角度
    animation.
autoreverses=YES;
   
return animation;
}


0 0
原创粉丝点击