2012-10-30 13 views
0

我有這個頭髮拉問題,我希望有人能幫助我。它看起來像一個小東西,但我一直無法找到任何解決方案。UIButton總是回到它的第一個位置?

我有一個UIButton,我想要移動一個動畫塊。每當我點擊按鈕我將它移動到一個新的位置,按鈕總是移動到正確的位置,問題是它總是從第一個位置移動到新的位置。

讓我們說,我有一個按鈕,在(0,0)我然後把它移動到(0,50)作品像一個漂亮! 現在我想將按鈕移動到(0,100),我預計它會從(0,50)動畫到(0,100),但是它會從(0,0)動畫到(0,100),因此它會在正確的位置結束,但會從錯誤的位置移動。

我通過在我的情況下在一個動畫塊

[UIView animateWithDuration:0.5f 
           delay:0.0f 
          options:UIViewAnimationCurveLinear 
         animations:^{ 
         } 
         completion:nil]; 

設置一個新的幀移動UIButton我在完成塊移動的按鈕,當其他控制成品移動。

我真的很希望有人知道我在說什麼,併爲我提供答案。

我用動畫的UIButton的是這樣的代碼,該按鈕的名稱是「btnAddPhoneNumber」,其也是「發件人」

[UIView animateWithDuration:1 
         delay:0 
        options:UIViewAnimationCurveLinear 
       animations:^{ 
        txtNewPhoneNumber.frame = CGRectMake(newXnumber, newYnumber, txtPhoneNumber.frame.size.width, txtPhoneNumber.frame.size.height); 

       } 
       completion:^(BOOL finished) { 
        txtNewPhoneNumber.placeholder = @"here you go!"; 

        //animate add textfield button. 
        [UIView animateWithDuration:0.5f 
              delay:0 
             options:UIViewAnimationCurveLinear 
             animations:^{ 



              [sender setFrame:CGRectMake(btnAddPhoneNumber.frame.origin.x, newYnumber, btnAddPhoneNumber.frame.size.width, btnAddPhoneNumber.frame.size.height)]; 

              txtNewPhoneNumberTitle.frame = CGRectMake(newXtitle, newYtitle, txtPhoneNumberTitle.frame.size.width, txtPhoneNumberTitle.frame.size.height); 

              [btnDelete setFrame:btnDeleteRect]; 
             } 
             completion:^(BOOL finished) { 
             }]; 
       }]; 

感謝您閱讀我的問題,並幫助我找到一個解決方案。

編輯(添加的代碼示例)

+0

您可以發佈您爲兩者編寫的代碼嗎? – iDev

+0

嗨ACB,我已添加完整的動畫塊。 – MortenHN

+0

在完成塊中,您可以在控制檯中打印txtNewPhoneNumber的幀,並檢查幀在完成塊中時是否正確更新了幀? – iDev

回答

0

嘗試增加UIViewAnimationOptionBeginFromCurrentState到第二動畫的選項,看它是否解決您的問題。

[UIView animateWithDuration:0.5f 
           delay:0.0f 
          options:UIViewAnimationCurveLinear | UIViewAnimationOptionBeginFromCurrentState 
         animations:^{ 
         } 
         completion:nil]; 

另一種方法去。將在所述第二動畫的完成處理程序再次設置按鈕的幀(爲相同的值)。

+0

嗨Tobi。我已經嘗試過兩種方式,它沒有任何不同,它仍然從原始框架中獲得動畫。 – MortenHN

+0

您能否提供所有相關的代碼?包括按鈕的動作和設置'newYnumber'的值的部分 – Tobi

0

檢查此變通。正如我在評論中提到的,您可以將其用作最後一個選項。不是推薦的方法。

[UIView animateWithDuration:1 
         delay:0 
        options:UIViewAnimationCurveLinear 
       animations:^{ 
        txtNewPhoneNumber.frame = CGRectMake(newXnumber, newYnumber, txtPhoneNumber.frame.size.width, txtPhoneNumber.frame.size.height); 

       } 
       completion:^(BOOL finished) { 
        txtNewPhoneNumber.placeholder = @"here you go!"; 

        [self performSelector:@selector(callSecondAnimation) withObject:nil afterDelay:1.5f)]; //or 0.5f 
       }]; 

- (void)callSecondAnimation { 
    //animate add textfield button. 
    [UIView animateWithDuration:0.5f 
          delay:0 
         options:UIViewAnimationCurveLinear 
        animations:^{ 

         [sender setFrame:CGRectMake(btnAddPhoneNumber.frame.origin.x, newYnumber, btnAddPhoneNumber.frame.size.width, btnAddPhoneNumber.frame.size.height)]; 

         txtNewPhoneNumberTitle.frame = CGRectMake(newXtitle, newYtitle, txtPhoneNumberTitle.frame.size.width, txtPhoneNumberTitle.frame.size.height); 

         [btnDelete setFrame:btnDeleteRect]; 
        } 
        completion:^(BOOL finished) { 
        }]; 
} 

更新:

試試這個第一,

[UIView animateWithDuration:1 
         delay:0 
        options:UIViewAnimationCurveLinear 
       animations:^{ 
        txtNewPhoneNumber.frame = CGRectMake(newXnumber, newYnumber, txtPhoneNumber.frame.size.width, txtPhoneNumber.frame.size.height); 

       } 
       completion:^(BOOL finished) { 
        txtNewPhoneNumber.placeholder = @"here you go!"; 
        txtNewPhoneNumber.frame = CGRectMake(newXnumber, newYnumber, txtPhoneNumber.frame.size.width, txtPhoneNumber.frame.size.height);//try setting it here once and then call next one 
        [self callSecondAnimation]; 
       }]; 

- (void)callSecondAnimation { 
    //animate add textfield button. 
    [UIView animateWithDuration:0.5f 
          delay:0 
         options:UIViewAnimationCurveLinear 
        animations:^{ 

         [sender setFrame:CGRectMake(btnAddPhoneNumber.frame.origin.x, newYnumber, btnAddPhoneNumber.frame.size.width, btnAddPhoneNumber.frame.size.height)]; 

         txtNewPhoneNumberTitle.frame = CGRectMake(newXtitle, newYtitle, txtPhoneNumberTitle.frame.size.width, txtPhoneNumberTitle.frame.size.height); 

         [btnDelete setFrame:btnDeleteRect]; 
        } 
        completion:^(BOOL finished) { 
        }]; 
} 
+0

這兩個建議都沒有任何區別。你知道是什麼導致這個問題,因爲你提出這些方法?感謝您花時間幫助,真的很感激! – MortenHN

+0

我的猜測是,在上一個動畫完成之前完成塊被調用。所以計劃是在這兩種動畫之間引入一種延遲,讓用戶感覺它是連續的。所以如果你只是刪除第二個動畫,第一個動畫是否正常工作以移動到所需點? – iDev

+0

我明白你的意思,但可悲的是,似乎並沒有解決我的問題。我發現這很奇怪! – MortenHN

0

我發現這個問題,使用自動版式視圖,當我在自動版式去除蜱我的按鈕的新位置保存,並且下一個動畫從之前的位置繼續。

感謝ACB和Tobi的投入,他們幫助我嘗試了不同的解決方案,最終解決了問題!

相關問題