将动画设置为UIView为默认状态

时间:2014-08-07 00:03:29

标签: ios uiview uianimation

我的一个视图控制器中有一个动画正弦波,每次检测到向上滑动手势时都会出现,并在检测到向下滑动时消失。正弦波是一个UIView,动画将正弦波转换为屏幕。这在我第一次上下滑动时工作正常。但是,在第一次刷卡后我得到了各种各样的问题。我注意到,如果我在每次向下滑动后重新分配并初始化正弦波,我可以让它正常工作,但我知道这不是正确的修复。我的假设是正弦波的帧必须重置为原来的帧,但我这样做的尝试并没有解决问题(我的尝试在下面的代码中被注释掉了)。有任何想法吗?

查看控制器代码:

 - (void)viewDidLoad
{
    [self setUpSine];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
}


-(void) setUpSine
{

    self.sineWave = [[OSDrawWave alloc] initWithFrame:CGRectMake(-1*self.view.bounds.size.width, self.view.bounds.size.height, 2*self.view.bounds.size.width, .3125*(2*self.view.bounds.size.width))];

    [self.sineWave setBackgroundColor:[UIColor clearColor]];

    [self.view insertSubview:self.sineWave belowSubview:self.panedView];

}

- (IBAction)handleUpSwipe:(UISwipeGestureRecognizer *)recognizer
{
     [self.sineWave animateWave];
     [self showSine];

     [UIView animateWithDuration:.25 delay:0 options:UIViewAnimationOptionCurveLinear animations:^
     {
         self.sineWave.frame = CGRectOffset(self.sineWave.frame, 0, -246);

      } 
      completion:^(BOOL finished)
      {
         NSLog(@"sine wave y position AFTER UP swipe %f", self.sineWave.frame.origin.y);

      }];
    }

- (IBAction)handleDownSwipe:(UISwipeGestureRecognizer *)recognizer
{
     [self hideSine];

    if(self.panedView.frame.origin.y <0)
    {

    [UIView animateWithDuration:.25 delay:0 options:UIViewAnimationOptionCurveLinear animations:^
    {
        self.sineWave.frame = CGRectOffset(self.sineWave.frame, 0, 246);

    } 
    completion:^(BOOL finished)
    {

    }];

    }
}

-(void) showSine
{
    [self.sineWave setHidden:NO];
    [self.sineWave fadeInAnimation];

} 

-(void) hideSine
{
    [self.sineWave fadeOutAnimation];
    [self.sineWave setHidden:YES];
}  

-(void) appDidEnterForeground:(NSNotification *)notification
{
     [self.sineWave animateWave];
}

我的正弦波是UIView的子类。这是sineWave类中的代码

- (void)animateWave {

    [self.layer removeAllAnimations];
    self.transform=CGAffineTransformIdentity;

    [UIView animateWithDuration:3 delay:0.0 options: UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction  animations:^{

        self.transform = CGAffineTransformMakeTranslation(self.frame.size.width/2,0);
} completion:^(BOOL finished) {
        self.transform = CGAffineTransformMakeTranslation(-self.frame.size.width/2, 0);
    }];
}

-(void) fadeInAnimation
{
    [UIView animateWithDuration:0 animations:^{
    self.alpha = 1.0;}];

}

-(void) fadeOutAnimation
{
    [UIView animateWithDuration:3 animations:^{
    self.alpha = 0.0;}];

}

- (void)drawRect:(CGRect)rect
{
self.yc = 30;//The height of a crest.
float w = 0;//starting x value.
float y = rect.size.height;
float width = rect.size.width;
int cycles = 6;//number of waves
self.x = width/cycles;
CGContextRef context = UIGraphicsGetCurrentContext();
CGMutablePathRef path = CGPathCreateMutable();
CGContextSetLineWidth(context, 2);
while (w <= width) {
    CGPathMoveToPoint(path, NULL, w,y/2);
    CGPathAddQuadCurveToPoint(path, NULL, w+self.x/4, y/2 - self.yc, w+self.x/2, y/2);
    CGPathAddQuadCurveToPoint(path, NULL, w+3*self.x/4, y/2 + self.yc, w+self.x, y/2);
    w+=self.x;
}
CGContextAddPath(context, path);
[[UIColor colorWithRed:107/255.0f green:212/255.0f blue:231/255.0f alpha:1.0f]setStroke];
CGContextDrawPath(context, kCGPathStroke);
self.alpha= 0.0;
}

1 个答案:

答案 0 :(得分:0)

由于来自this answer

的线索,我能够实现这一目标

在视图控制器中,您需要订阅UIApplicationWillEnterForegroundNotification以在应用从后台返回时收到通知。我更改了视图控制器中的viewDidLoad并添加了一个处理通知的方法 -

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setUpSine];
    [self.sineWave animateWave];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
}

-(void) appDidEnterForeground:(NSNotification *)notification
{
    [self.sineWave animateWave];    
}

我还从animateWave方法

中删除了对handleUpSwipe:的调用
- (IBAction)handleUpSwipe:(UISwipeGestureRecognizer *)recognizer
{
     [self showSine];

     [UIView animateWithDuration:.25 delay:0 options:UIViewAnimationOptionCurveLinear animations:^
     {
         self.sineWave.frame = CGRectOffset(self.sineWave.frame, 0, -246);

      } 
      completion:^(BOOL finished)
      {
         NSLog(@"sine wave y position AFTER UP swipe %f", self.sineWave.frame.origin.y);

      }];
    }

您还需要更改animateWave以在动画(重新)开始之前清除任何先前的动画。完成块中不需要任何内容​​ -

- (void)animateWave {

    [self.layer removeAllAnimations];
    self.transform=CGAffineTransformIdentity;

    [UIView animateWithDuration:3 delay:0.0 options: UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveLinear |UIViewAnimationOptionAllowUserInteraction animations:^{

        self.transform = CGAffineTransformMakeTranslation(self.frame.size.width/2,0);
    } completion:^(BOOL finished) {
    }];
}