【问题标题】:Proper Segue from Array of UIImages to a View Controller从 UIImages 数组到视图控制器的正确 Segue
【发布时间】:2016-01-01 00:13:03
【问题描述】:

我尝试使用this 线程寻求帮助,但代码不起作用...

我有一组动画图像(每 3 秒向左滚动一次)。我想要做的是,当您单击相应的图像时,它将显示该图像的详细视图,该视图将在 VC 上带有标签,其中包含有关该图像的一些详细信息。但是,这就是我卡住的地方。

我已经应用了 UITapGestureRecognizer 的使用,它确实推送到了新的视图控制器,但它是一个静态推送——这意味着无论我在相应的 VC 页面上单击什么图像都是相同的。

我的代码如下

主视图控制器:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var sponsorAnimation: UIImageView!

let images = [
    UIImage(named: "image1")!,
    UIImage(named: "image2")!]

var index = 0
let animationDuration: NSTimeInterval = 1
let switchingInterval: NSTimeInterval = 3

let tappedSponsor = UITapGestureRecognizer()

override func viewDidLoad() {
    super.viewDidLoad()

    navigationController?.navigationBar.barTintColor = UIColor(red: 119/255, green: 136/255, blue: 153/255, alpha: 1.0)
    navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]

    sponsorAnimation.image = images[index]
    animateImageView()

    tappedSponsor.addTarget(self, action: "imageTapped")
    sponsorAnimation.addGestureRecognizer(tappedSponsor)
    sponsorAnimation.userInteractionEnabled = true

}

func imageTapped() {
// not sure what code to enter in here...
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if (segue == "showSponsor") {
        let destinationVC = segue.destinationViewController as! NewViewController

        destinationVC.sponsorImage = images
        // am i on the right track??

    }
}

func animateImageView() {
    CATransaction.begin()

    CATransaction.setAnimationDuration(animationDuration)
    CATransaction.setCompletionBlock {
        let delay = dispatch_time(DISPATCH_TIME_NOW, Int64(self.switchingInterval * NSTimeInterval(NSEC_PER_SEC)))
        dispatch_after(delay, dispatch_get_main_queue()) {
            self.animateImageView()
        }
    }

    let transition = CATransition()
    transition.type = kCATransitionPush
    transition.subtype = kCATransitionFromRight
    /*
    transition.type = kCATransitionPush
    transition.subtype = kCATransitionFromRight
    */
    sponsorAnimation.layer.addAnimation(transition, forKey: kCATransition)
    sponsorAnimation.image = images[index]

    CATransaction.commit()

    index = index < images.count - 1 ? index + 1 : 0

}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

这是我目前的详细 VC:

import UIKit

class NewViewController: UIViewController {

@IBOutlet weak var sponsorAnimation: UIImageView!


var sponsorImage: UIImage!


override func viewDidLoad() {
    super.viewDidLoad()

    self.sponsorAnimation!.image = self.sponsorImage

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

【问题讨论】:

  • 您好,有几个问题:谁在执行segue?您是否在情节提要中将其配置为自动执行?在我看来,你应该有一个像 VC->VC 这样的 segue,而不是 button->VC,在 imageTapped 方法中你应该执行 segue
  • @pedros 目前我有一个从 TapGestureRecognizer(附加到 imageView)到 NewViewController 的 segue。如果我做 VC -> VC,有没有办法可以传递选定的图像?我假设您可以使用带有图像名称的 if 语句 - 在本例中为 image1 和 image2。但正如你所见,我很迷茫!
  • 您是否在if (segue == "showSponsor") 处设置了断点?它是否完全进入了if?如果是这样,你就在正确的轨道上。你需要的是保存被点击的图像,所以在imageTapped你需要找到被点击的图像并保存在一个变量中,这样你就可以将这个图像传递给nextVC。您可以看到在prepareForSegue 中您将两个图像都传递给了 nextVC
  • @pedros 听起来很傻,但我对 Xcode 还是很陌生...我在 if (segue == "showSponsor") 处设置了一个断点。如何检查它是否注册?
  • 别担心,我们都需要在某个时候学习。尝试将断点放在 if 中,然后运行您的应用程序,然后点击图像。如果代码在您的断点处停止而不是按预期进入 if。

标签: ios xcode swift uiviewcontroller segue


【解决方案1】:

好吧,我们无法从动画图像视图中获取当前图像。您需要其他方法来做到这一点。可能通过为动画添加NSTimer。这是一个objective C thread 可以帮助你。不过,您需要将其转换为 Swift,我相信您可以做到这一点:)!

【讨论】:

    猜你喜欢
    • 2012-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-13
    • 2013-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多