簡體   English   中英

按下按鈕旋轉圖像

[英]rotate image on button press

我正在嘗試旋轉並按下按鈕時成像180度。 現在,我可以將其旋轉180度,但是當我再次按下該按鈕時,圖像不會向后旋轉。 我覺得我缺少一些簡單的東西。

if語句有效,我只是簡化了代碼,只保留了無效的部分。

        if(self.dropdownConstraint.constant == -78)
    {
        //roate arrow up
        UIView.animateWithDuration(0.5, animations: {
            self.arrow.transform = CGAffineTransformMakeRotation((180.0 * CGFloat(M_PI)) / 180.0)
            self.view.layoutIfNeeded()
        })
    }
    else
    {
        //roate arrow down
        UIView.animateWithDuration(0.5, animations: {
            self.arrow.transform = CGAffineTransformMakeRotation((-180.0 * CGFloat(M_PI)) / -180.0)
            self.view.layoutIfNeeded()
        })            
    }

提前致謝!

變換不是累積的,因此第一個旋轉180度(從0開始),第二個旋轉-180度(也從零開始),看起來是一樣的。 只需修改反向變換即可將旋轉度設置回零:

    UIView.animateWithDuration(0.5, animations: {
        self.arrow.transform = CGAffineTransformMakeRotation(0)
        self.view.layoutIfNeeded()
    })

將轉換設置為標識以撤消之前的轉換:

UIView.animateWithDuration(0.5, animations: {
    self.arrow.transform = CGAffineTransformIdentity;
    self.view.layoutIfNeeded()
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM