2013-09-22 111 views
0

我有100個圖像(a400e3x_0.png,.....,a400e3x_98.png,a400e3x_99.png)。 這些圖像根據UIImageView上的滑動而改變,就像用戶可以控制的動畫一樣。 問題是,當用戶停止平移,然後再次啓動,動畫回到圖像0.我需要保存位置,當用戶停止平移,我可以做到這一點。但是當我添加它時,添加總是在那裏。例如:我在Image 16停止平移,然後從16開始,當(curX%100)爲零時,它仍然總是添加16。你能幫我算一下這個算法嗎?圖像序列與平移手勢algorthm

- (IBAction)PanGesAction:(id)sender 
{ 
CGPoint translation = [_PanGes translationInView:self.view]; 
int curX = (int)translation.x; 
addition = (curX%100)+Savedif; 

NSLog(@"%d", addition); 

[_ImageV2 setImage:[UIImage imageNamed:[NSString stringWithFormat:@"a400e3x_%d.png", addition]]]; 


    if (_PanGes.state == UIGestureRecognizerStateEnded) 
    { 
     NSLog(@"End"); 
     Savedif = addition; 
     //When user end 

    } 

    if (_PanGes.state == UIGestureRecognizerStateBegan) 
    { 
     NSLog(@"Began"); 
     //When user begin 

    } 
} 
+0

您已經檢測到'UIGestureRecognizerStateBegan',爲什麼不使用它來決定如何管理'addition'? – Wain

+0

我可以,但如何? – Dim

回答

1

當一個新的手勢開始,你必須編輯它會送你價值的機會,因此,我們可以將您的初始值傳送到姿態,讓事情變得簡單:

- (IBAction)PanGesAction:(id)sender 
{ 
    if (_PanGes.state == UIGestureRecognizerStateBegan) 
    { 
     NSLog(@"Began"); 
     //When user begin 
     [_PanGes setTranslation:(CGPoint){Savedif, 0} inView:self.view]; 
     Savedif = 0; 
    } 

    CGPoint translation = [_PanGes translationInView:self.view]; 
    int curX = (int)translation.x; 
    addition = (curX%100); 

    NSLog(@"%d", addition); 

    [_ImageV2 setImage:[UIImage imageNamed:[NSString stringWithFormat:@"a400e3x_%d.png", addition]]]; 


    if (_PanGes.state == UIGestureRecognizerStateEnded) 
    { 
     NSLog(@"End"); 
     Savedif = addition; 
     //When user end 
    } 
} 
+0

Thanx!我一直堅持這幾個小時! – Dim

-1

有是一種方式,你不必保存任何東西,

介紹在UIViewtransform財產,

試試這個代碼:

- (void)handlePanGesture:(UIPanGestureRecognizer *)gesture 
{ 
    UIView *imageView = gesture.view; 
    if (gesture.state == UIGestureRecognizerStateChanged) { 
     CGPoint transition = [gesture translationInView:gesture.view]; 
     imageView.transform = CGAffineTransformMakeTranslation(imageView.transform.tx + transition.x, imageView.transform.tx + transition.y); 
     [gesture setTranslation:CGPointZero inView:gesture.view]; 
    } 
    else if (gesture.state == UIGestureRecognizerStateEnded) { 
     imageView.center = CGPointMake(imageView.center.x + imageView.transform.tx, imageView.center.y + imageView.transform.ty); 
     imageView.transform = CGAffineTransformIdentity; 
    } 
} 

的ImageView的立場始終是:

imageView.center.x+image.transform.tx 
imageView.center.y+image.transform.ty 

你可以在它採取相應的行動。