如何在iOS中轻松制作动画?

时间:2014-04-01 22:02:50

标签: ios objective-c animation uiview

我正在尝试为录像机上的红点设置动画。我希望点每隔0.7秒突然出现并消失。我尝试的每个动画选项都以某种方式放入或缩小点。我怎样才能使它完全显现并完全消失?看起来UIViewAnimationOptionTransitionNone默认与UIView.h中的UIViewAnimationOptionCurveEaseInOut值相同。如何在没有任何宽松的情况下制作动画?

enum {
   UIViewAnimationOptionLayoutSubviews            = 1 <<  0,
   UIViewAnimationOptionAllowUserInteraction      = 1 <<  1,
   UIViewAnimationOptionBeginFromCurrentState     = 1 <<  2,
   UIViewAnimationOptionRepeat                    = 1 <<  3,
   UIViewAnimationOptionAutoreverse               = 1 <<  4,
   UIViewAnimationOptionOverrideInheritedDuration = 1 <<  5,
   UIViewAnimationOptionOverrideInheritedCurve    = 1 <<  6,
   UIViewAnimationOptionAllowAnimatedContent      = 1 <<  7,
   UIViewAnimationOptionShowHideTransitionViews   = 1 <<  8,
   UIViewAnimationOptionOverrideInheritedOptions  = 1 <<  9,

   UIViewAnimationOptionCurveEaseInOut            = 0 << 16,
   UIViewAnimationOptionCurveEaseIn               = 1 << 16,
   UIViewAnimationOptionCurveEaseOut              = 2 << 16,
   UIViewAnimationOptionCurveLinear               = 3 << 16,

   UIViewAnimationOptionTransitionNone            = 0 << 20,
   UIViewAnimationOptionTransitionFlipFromLeft    = 1 << 20,
   UIViewAnimationOptionTransitionFlipFromRight   = 2 << 20,
   UIViewAnimationOptionTransitionCurlUp          = 3 << 20,
   UIViewAnimationOptionTransitionCurlDown        = 4 << 20,
   UIViewAnimationOptionTransitionCrossDissolve   = 5 << 20,
   UIViewAnimationOptionTransitionFlipFromTop     = 6 << 20,
   UIViewAnimationOptionTransitionFlipFromBottom  = 7 << 20,
};

现在是我的代码,

[UIView animateWithDuration:0.7
                          delay:0
                        options:(UIViewAnimationOptionRepeat | UIViewAnimationOptionTransitionNone)
                     animations:^(void){
                         self.recordingAnimationImage.alpha=0.0;
                                        }
                     completion:nil
                     ];

3 个答案:

答案 0 :(得分:3)

不要动画。如果您希望它立即出现并消失,请尝试以下方法:

viewcontroller.m

@property (strong, nonatomic) NSTimer *timer;

- (void)viewDidLoad {
    if (!self.timer) {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:0.7 target:self selector:@selector(alternate) userInfo:nil repeats:YES];
    }
}

- (void)alternate {
        self.myDotView.hidden = !self.myDotView.hidden;
}

- (void)viewWillDisappear {
    [self.timer invalidate];
    self.timer = nil;
}

答案 1 :(得分:0)

在iOS 7及更高版本中,您可以使用关键帧动画。

[UIView animateKeyframesWithDuration:1.4 delay:0.0 options:UIViewKeyframeAnimationOptionRepeat animations:^{
    [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.0 animations:^{
        recordingAnimationImage.alpha = 1.0;
    }];
    [UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.0 animations:^{
        recordingAnimationImage.alpha = 0.0;
    }];
} completion:nil];

通过为第二个状态更改指定0.5的相对开始时间,这意味着它在整个1.4秒的持续时间中间发生。通过指定零relativeDuration,这意味着状态之间的转换是瞬时的。

答案 2 :(得分:0)

如果您使用的是Swift 3,则可以使用UIViewAnimationOptionCurveLinear(或简称为.curveLinear)轻松地为UIView设置动画:

UIView.animate(withDuration: 2, delay: 0, options: [.curveLinear], animations: { 
            //Do what you want
        }, completion: nil)

如果你想完成:

    UIView.animate(withDuration: 2, delay: 0, options: [.curveLinear], animations: { 
        //Do what you want
    }) { (success: Bool) in
        //Completion here
    }