gpt4 book ai didi

ios - 带有 NSTimer 问题的 UIPanGesture

转载 作者:行者123 更新时间:2023-11-28 12:53:09 25 4
gpt4 key购买 nike

我遇到了一个问题我有一个 View Controller ,它包含一个平移手势和一个 NSTimer,它有一个每秒调用的选择器问题是每次激活计时器时,我尝试使用平移手势移动的标签都会自动返回到其原始位置。可能是什么原因?

这是代码

import UIKit

class gameViewController: UIViewController {


@IBOutlet weak var img1: UIImageView!
@IBOutlet weak var lbl1: UILabel!


@IBOutlet weak var timerLabel: UILabel!
@IBOutlet weak var gameDifficultyLabel: UILabel!

var timer = NSTimer()
var timeManager = TimerManager()

override func viewDidLoad() {
super.viewDidLoad()
timerLabel.text = "\(timeManager.displayedTime(timeManager.time!))"
// Do any additional setup after loading the view.
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "updateTime", userInfo: nil, repeats: true)
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if let difficulty = timeManager.difficulties {
switch difficulty {
case .Easy:
gameDifficultyLabel.text = "Easy"
case .Medium:
gameDifficultyLabel.text = "Medium"
case .Hard:
gameDifficultyLabel.text = "Hard"
}
}
}

@IBAction func panGesture(sender: UIPanGestureRecognizer) {
let translation = sender.translationInView(self.view)
if let view = sender.view {
view.center = CGPoint(x:view.center.x + translation.x,
y:view.center.y + translation.y)
}
sender.setTranslation(CGPointZero, inView: self.view)
}


override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func updateTime() {
let shouldStop = timeManager.updateTime()
timerLabel.text = timeManager.displayedTime(timeManager.time)
if shouldStop {
timer.invalidate()
}
}
}

更新:我已经更新了代码。以编程方式添加标签解决了这个问题,但我有另一个问题这是代码。

类游戏 View Controller :UIViewController {

@IBOutlet var numbersContainers: [UIImageView]!
@IBOutlet var numbersLabel: [UILabel]!

@IBOutlet weak var timerLabel: UILabel!
@IBOutlet weak var gameDifficultyLabel: UILabel!

var LabelsBackground = [UIImageView]()
var Labels = [UILabel]()

var timer = NSTimer()
var timeManager = TimerManager()

var panGesture : UIPanGestureRecognizer!
override func viewDidLoad() {
super.viewDidLoad()
timerLabel.text = "\(timeManager.displayedTime(timeManager.time!))"
self.createLabelsBackground()
self.setUpLabelsBackground()
self.addLabelsBackground()

self.createLabels()
self.setUpLabels()
self.addLabels()

panGesture = UIPanGestureRecognizer(target: self, action: "panAction:")
panGesture.minimumNumberOfTouches = 1
panGesture.maximumNumberOfTouches = 1

}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "updateTime", userInfo: nil, repeats: true)
self.addPanGesture()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if let difficulty = timeManager.difficulties {
switch difficulty {
case .Easy:
gameDifficultyLabel.text = "Easy"
case .Medium:
gameDifficultyLabel.text = "Medium"
case .Hard:
gameDifficultyLabel.text = "Hard"
}
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func updateTime() {
let shouldStop = timeManager.updateTime()
timerLabel.text = timeManager.displayedTime(timeManager.time)
if shouldStop {
timer.invalidate()
}
}
func createLabelsBackground() {
for _ in 0..<10 {
if let img = UIImage(named: "noContainer") {
LabelsBackground.append(UIImageView(image: img))
}

}
}
func setUpLabelsBackground() {
for i in 0..<10 {
LabelsBackground[i].frame = numbersContainers[i].frame
LabelsBackground[i].center = numbersContainers[i].center
}
}
func addLabelsBackground() {
for background in LabelsBackground {
self.view.addSubview(background)
}
}

func createLabels() {
for _ in 0..<10 {
Labels.append(UILabel())
}
}
func setUpLabels() {
for i in 0..<10 {
Labels[i].frame = numbersLabel[i].frame
Labels[i].center = numbersLabel[i].center
Labels[i].backgroundColor = UIColor.whiteColor()
Labels[i].userInteractionEnabled = true
}
}
func addLabels() {
for label in Labels {
self.view.addSubview(label)
}
}

func addPanGesture(){
for label in Labels {
label.text = "10"
label.addGestureRecognizer(panGesture)
}
}

func panAction(recognizer: UIPanGestureRecognizer) {
print("panAction")
let translation = recognizer.translationInView(self.view)
if let myView = recognizer.view {
myView.center = CGPoint(x: myView.center.x + translation.x, y: myView.center.y + translation.y)
}
recognizer.setTranslation(CGPointZero, inView: self.view)
}

只是为了澄清@IBOutlet var numbersLabel:[UILabel]!var 标签 = UILabel

numbersLabels 是使用 Storyboard添加的空标签,仅用于在以编程方式创建标签时定位。

问题是以编程方式创建并添加到标签数组的标签没有响应。

最佳答案

我认为您确实启用了自动布局(默认情况下启用)。如果您没有为 View 指定任何约束(例如您的标签),Storyboard 将在构建时为您创建它们。您可以通过查看标签的 Size Inspector 设置来验证这一点。

调用 updateTime() 时,自动布局会根据 Storyboard自动为其创建的约束将您的标签放回其原始位置。

要解决这个问题,您可以:

1) 以编程方式创建标签,而不是在 Storyboard 中创建标签。

2) 为 Storyboard 中的标签位置添加显式约束,然后为这些约束创建 @IBOutlet 并通过更改 constant 属性在代码中移动标签这些约束而不是改变它的框架(中心)。

关于ios - 带有 NSTimer 问题的 UIPanGesture,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35815428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com