gpt4 book ai didi

ios - 自定义 segue,扩展源 View Controller

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

尝试编写自定义 segue,其中源 View 被缩放,同时更改目标的 alpha 以淡入目标。目标是 MKMapView,所以我希望它在淡入淡出时更新。
根据我的尝试,我最终同时扩展了源和名称,而我无法只扩展源 View 。

    class Map_Segue: UIStoryboardSegue {

override func perform()
{
var sourceViewController : UIViewController = self.sourceViewController as UIViewController
var destinationViewController : UIViewController = self.destinationViewController as UIViewController
destinationViewController.view.alpha = 0 sourceViewController.addChildViewController(destinationViewController) sourceViewController.view.addSubview(destinationViewController.view)
UIView.animateWithDuration(2.0,delay:1.0,options: UIViewAnimationOptions.CurveEaseInOut, // delay of 1 second for aesthetics
animations:
{
sourceViewController.view.transform = CGAffineTransformScale(sourceViewController.view.transform, 100.0, 100.0);
destinationViewController.view.alpha = 1;
},
completion:
{ (finished:Bool)->Void in
destinationViewController.didMoveToParentViewController(sourceViewController);
}
)
}
}

我试过 autoresizesSubviews=false,但似乎没有任何作用。

我尝试将动画中的目标变换设置为 1/100(并将选项设置为 UIViewAnimationOptions.CurveLinear,最终结果正确,但过渡效果错误( map 在背景中放大然后再次缩小)我确信这应该很容易,而且我是新手,所以我错过了一个技巧。有人有什么想法吗?

更新:

我发现(有点明显)我应该使用 sourceViewController.view.superview?.insertSubview( destinationViewController.view, atIndex:0) 将它插入原始源 View 旁边,而不是作为它的 child ,显然,转换是独立的,而不是相对于父 View (它将作为 subview )。然后问题是切换到新 View 。使用我的方法,不会调用 viewWillAppear 和类似的方法,并且更改 View 不起作用。如果我调用 presentViewController,那么在调用 viewWillAppear 时会出现故障。

目前的解决方案

忘记使用自定义转场吧。按照建议将 UIImageView 放置在 map View 的顶部,并在大约 5 分钟的编码中实现了漂亮的动画淡入淡出。

最佳答案

我认为您对父 View Controller 和 subview Controller 等有点困惑。临时将第二个 View Controller 的 View 添加到第一个 View Controller ,执行转换然后清理就足够了。

我在一个简单的示例项目中测试了以下内容。请注意,我将第二个 View Controller 的 View 添加到窗口而不是第一个 View Controller 的 View ,否则它也会按比例放大。

override func perform() {
let first = self.sourceViewController as ViewController
let second = self.destinationViewController as ViewController

second.view.alpha = 0
first.view.window!.addSubview(second.view)

UIView.animateWithDuration(2.0, delay: 0.0,
options: .CurveEaseInOut, animations: { () -> Void in
first.view.transform = CGAffineTransformMakeScale(100.0, 100.0)
second.view.alpha = 1.0
})
{ (finished: Bool) -> Void in
second.view.removeFromSuperview()
first.presentViewController(second, animated: false, completion: nil)
}
}

关于ios - 自定义 segue,扩展源 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26337029/

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