저는 뷰 컨트롤러 내부의 뷰에 대해 랜덤 애니메이션을 테스트 해 왔습니다. 한 번 실행하면 잘 작동하는 것 같습니다. 그러나 코드를 while 루프에 넣으면 응용 프로그램이 충돌합니다. CPU 사용률이 100 %로 향상됩니다. 이는 무작위 좌표가 일정하게 생성되기 때문입니다. 응용 프로그램을 손상시키지 않고 루프 된 임의의 애니메이션을 만들기 위해이 코드를 포맷하는 가장 좋은 방법은 무엇입니까?Swift에서 루핑 랜덤 애니메이션 메서드 2.2
func randomAnimationForPostPackets() {
func completionMethodX() -> CGFloat {
let randomCoordinatesXInt: UInt32 = arc4random_uniform(300)
let randCoordsX = CGFloat(randomCoordinatesXInt)
return randCoordsX
}
func completionMethodY() -> CGFloat {
let randomCoordinatesYInt: UInt32 = arc4random_uniform(300)
let randCoordsY = CGFloat(randomCoordinatesYInt)
return randCoordsY
}
UIView.animateWithDuration(5, animations: {
self.postPacketView.center.x = completionMethodX()
self.postPacketView.center.y = completionMethodY()
},completion: {(finished:Bool) in
completionMethodX()
completionMethodY()
})
}
이 방법도록 뷰 컨트롤러에서의 viewDidLoad 방법으로 구현된다
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.sharedApplication().statusBarStyle = .LightContent
webBackground()
createBlurEffectView()
let randomPositionX : UInt32 = arc4random_uniform(300)
let randomPositionY : UInt32 = arc4random_uniform(300)
let randomPositionCGX : CGFloat = CGFloat(randomPositionX)
let randomPositionCGY : CGFloat = CGFloat(randomPositionY)
postPacketView.frame = CGRectMake(randomPositionCGX, randomPositionCGY, 75, 75)
self.view.addSubview(postPacketView)
mainSegueButtonSetup()
**randomAnimationForPostPackets()**
userViewSetup()
self.definesPresentationContext = true
// Do any additional setup after loading the view.
}
상관 도움이 이해 될 것이다. UIView.animateWithDuration
방법에서
.Repeat 및 .Autoreverse 추가 : 1. 모호한 참조 오류를 만듭니다. 2. 새로운 임의의 좌표를 생성하지 않습니다. – Tofite
2) 이상한 중첩 함수 선언을했기 때문에 새로운 임의의 좌표를 생성하지 않습니다. completionMethodX와 Y의 두 함수를 제거하고 새로운 난수를 생성하는 변수에 코드를 추가하십시오. – Sean
함수 ** randomAnimationForPostPackets() **가 UIView 클래스 내에 정의되어 있습니까? – Sean