2015-03-02 61 views
3

我有一個UIImageView sliderPump這是我從右邊到屏幕無限次的左側移動通過調用兩種方法此起彼伏:的Objective-C:在塊動畫

-(void)pumpGoesRight 
    { 
     if (slide) 
     { 
      [UIView animateWithDuration:0.8 animations:^ 
      { 

       [sliderPump setFrame:CGRectMake(sliderPump.frame.origin.x+235, sliderPump.frame.origin.y, sliderPump.frame.size.width, sliderPump.frame.size.height)]; 

      } completion:^(BOOL finished) 
      { 
       [self pumpGoesLeft]; 
      }]; 
     } 
     else 
      return; 
    } 
    -(void)pumpGoesLeft 
    { 
     if (slide) 
     { 

      [UIView animateWithDuration:0.8 animations:^ 
      { 
       [sliderPump setFrame:CGRectMake(sliderPump.frame.origin.x-235, sliderPump.frame.origin.y, sliderPump.frame.size.width, sliderPump.frame.size.height)]; 

      } completion:^(BOOL finished) 
      { 
       [self pumpGoesRight]; 
      }]; 

      [self performSelector:@selector(pumpGoesRight) withObject:nil afterDelay:0.8]; 
     } 
     else 
      return; 

} 

我使用此代碼找到出的UIImageView sliderPumpX的立場,停止動畫:

-(void)stop 
{ 
    slide = NO; 
    NSLog(@"Slide Pump position: %f", sliderPump.layer.frame.origin.x); 
    [self.view.layer removeAllAnimations]; 
} 

的問題是,當我叫stop方法,我想拿到sliderPump的當前位置,但我即使動畫尚未完成,get也是動畫結尾的sliderPimp的最終位置。我怎樣才能獲得框架的當前位置?謝謝!

+3

這可能幫助:http://stackoverflow.com/questions/7624755/accessing-the-current-position-of-uiview-during-animation – 2015-03-02 19:03:31

+0

@JeffreyBerthiaume謝謝你,那個作品! – SmartTree 2015-03-02 19:12:51

回答

2

您應該使用表示層。

NSLog(@"Slide Pump position: %f", [sliderPump.layer.presentationLayer frame].origin.x);