在下面的代码中,当用户按住屏幕(longPressGestureRecognizer)时,我试图将CALayer从屏幕左侧动画到屏幕右侧.当用户抬起手指时,CALayer会暂停.
var l = CALayer()
var holdGesture = UILongPressGestureRecognizer()
let animation = CABasicAnimation(keyPath: "bounds.size.width")
override func didReceiveMemoryWarning(){
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
setUpView()
}
func setUpView(){
l.frame = CGRect(x: 0, y: 0, width: 0, height: 10)
l.backgroundColor = UIColor.redColor().CGColor
self.view.addGestureRecognizer(holdGesture)
holdGesture.addTarget(self, action:"handleLongPress:")
}
func handleLongPress(sender : UILongPressGestureRecognizer){
//User is holding down on screen
if(sender.state == .Began){ …Run Code Online (Sandbox Code Playgroud) 我的游戏中有很多角色,因此我有很多纹理.当加载纹理图集(包含大约5种不同的图像纹理)时,它会增加内存使用并将其保持在该数量.因此,越多的纹理就会持续驱动该数字,直到应用程序崩溃为止.我不需要同时拥有所有角色,我怎么可能在需要时加载一些角色纹理,当我不需要时释放其他角色,但是需要能够将其恢复原状.
您在下面看到的代码创建了一个 CALayer(矩形形状)并从左到右对其进行动画处理(您可以直接在新项目中复制并粘贴代码):
//Global Variables
var layer = CALayer()
var holdGesture = UILongPressGestureRecognizer()
let animation = CABasicAnimation(keyPath: "bounds.size.width")
func setUpView(){
self.view.addGestureRecognizer(holdGesture)
holdGesture.addTarget(self, action:"handleLongPress:")
}
func handleLongPress(sender : UILongPressGestureRecognizer){
//NEED IT HERE
//var layer = CALayer()
layer.frame = CGRect(x: 0, y: 0, width: 0, height: 10)
layer.backgroundColor = UIColor.redColor().CGColor
animation.fromValue = 0
animation.toValue = self.view.bounds.width * 2
animation.duration = 5
self.view.layer.addSublayer(layer)
if(sender.state == .Began){
print("Long Press Began")
layer.addAnimation(animation, forKey: "bounds.size.width")
}
else{
print("Long press ended")
pauseLayer(layer)
}
}
func pauseLayer(layer : CALayer){ …Run Code Online (Sandbox Code Playgroud) 我知道这之前已经发布过,但我是 swift 的新手,我想根据我的情况寻求帮助。所以我需要运行一个计时器,当 stopMusic() 被调用时(在我的应用程序中的某个地方),我需要暂停计时器,然后当 playMusic() 被调用时,我需要恢复计时器。
这是我所拥有的:
override func viewDidLoad() {
runFirstDropTimer = NSTimer.scheduledTimerWithTimeInterval(38.2, target: self, selector: "runAnimation", userInfo: nil, repeats: false)
}
func stopMusic() {
//Pause Timer
}
func playMusic() {
//Resume Timer
}
Run Code Online (Sandbox Code Playgroud)
感谢您提供任何帮助!!!
swift ×4
calayer ×2
ios ×2
animation ×1
caanimation ×1
nstimer ×1
sktexture ×1
sprite-kit ×1
xcode ×1