2016-07-18 5 views
0

어떻게 특정 CGPoint로 이동하려면 프로그래밍 방식으로 생성 된 객체에 애니메이션을 적용 할 수 있습니까? 이것은 CGAffineTransformMakeTranslation을 사용하지 않고, 이동하는 방법에 관한 것이기 때문에 정확히 어디로 옮길 것인가가 아닙니다. 내가 시도Swift, 특정 CGPoint에 애니메이션 적용

: translatesAutoresizingMaskIntoConstraints =

진정한는하지만 차이가 없습니다.

나는이 시도했다 : 시뮬레이터에서 실행할 때

//Define screen sizes 
let screenSize: CGRect = UIScreen.mainScreen().bounds 
let screenWidth = screenSize.width 
let screenHeight = screenSize.height 

// Sizes of centre cards 
let centreCardWidth = screenWidth/5 
let centreCardHeight = (screenWidth/5)/0.625 
let centralizeViewHorizontalCard = (screenWidth/2) - ((screenWidth/5)/2) 
let centralizeViewVerticleCard = screenHeight/2 

UIView.animateWithDuration(0.25, delay: 0.5, options: [], animations: { 
    centreCard3.frame = CGRectMake(centralizeViewHorizontalCard, centralizeViewVerticleCard, centreCard3.frame.size.width, centreCard3.frame.size.height) 
    }, completion: nil) 

그러나 절대적으로 아무 일도하지 않습니다.

+0

를 init이되고있다? – Chajmz

+0

언제이 코드를 호출하고 어디서 왔습니까? – jrturton

+0

에서 ViewDidLoad. – RufusV

답변

2

애니메이션을 적용 할 때 UI가 아직 렌더링되지 않았습니다. viewDidAppear에 넣으십시오. 이것은 나를 위해 작동 :

class ViewController: UIViewController { 

    @IBOutlet weak var centreCard3: UIView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 

    override func viewDidAppear(animated: Bool) { 
     let screenSize: CGRect = UIScreen.mainScreen().bounds 
     let screenWidth = screenSize.width 
     let screenHeight = screenSize.height 

     // Sizes of centre cards 

     let centralizeViewHorizontalCard = (screenWidth/2) - ((screenWidth/5)/2) 
     let centralizeViewVerticleCard = screenHeight/2 

     UIView.animateWithDuration(0.5, delay: 0.5, options: [], animations: { 
      self.centreCard3.frame = CGRectMake(centralizeViewHorizontalCard, centralizeViewVerticleCard, self.centreCard3.frame.size.width, self.centreCard3.frame.size.height) 
      }, completion: nil) 
    } 
} 

결과 : 당신의 centreCard3이

Result

관련 문제