从Open Class中的另一个VC调用函数 - Swift

时间:2017-10-01 20:21:22

标签: ios swift function

我正在尝试在Open Class中的另一个View Controller中调用一个函数。该函数实际上为我的视图添加了一个子视图。到目前为止,我在调用该函数时失败了。这是我的Open Class的代码:

open class ItemAnnotationView: MKAnnotationView {

override open func setSelected(_ selected: Bool, animated: Bool) {
    if selected {

        UIView.animate(withDuration: 0.1, animations: {

            print("show large icon")

            // smaller size
            self.mediumIconView.transform = CGAffineTransform(scaleX: 0.2, y: 0.2)
            self.mediumIconView.alpha = 0

        }, completion: { (animating) in


            // Start showing large icon
            self.largeIconView.alpha = 0.2


            // double size + show large icon
            UIView.animate(withDuration: 0.2, animations: {

                // double size
                self.largeIconView.transform = CGAffineTransform(scaleX: CGFloat(self.ENLARGE_MAX_SCALE), y: CGFloat(self.ENLARGE_MAX_SCALE))
                self.largeIconView.frame.origin.y = (1 - self.ENLARGE_MAX_SCALE) * self.MEDIUM_ICON_WIDTH - self.MEDIUM_ICON_WIDTH / 2 + 6
                // completely show large icon
                self.largeIconView.alpha = 1.0;

            }, completion: { (animating) in

                // a little smaller size
                UIView.animate(withDuration: 0.1, animations: {

                    // a little smaller size
                    self.largeIconView.transform = CGAffineTransform(scaleX: CGFloat(self.ENLARGE_FINAL_SCALE), y: CGFloat(self.ENLARGE_FINAL_SCALE))
                    self.largeIconView.frame.origin.y = (1 - self.ENLARGE_FINAL_SCALE) * self.MEDIUM_ICON_WIDTH - self.MEDIUM_ICON_WIDTH / 2 + 6
                }, completion: { (animating) in

                })

            })

        })

    } else {
        // normal size + hide large icon
        UIView.animate(withDuration: 0.2, animations: {

            print("show small icon")

            // normal size
            self.largeIconView.transform = CGAffineTransform(scaleX: 1, y: 1)

            // hide large icon
            self.largeIconView.alpha = 0.0;
            self.largeIconView.frame.origin.y = -self.MEDIUM_ICON_WIDTH / 2

        }, completion: { (animating) in

            // show medium/small icon
            self.mediumIconView.transform = CGAffineTransform(scaleX: 1, y: 1)
            self.mediumIconView.alpha = 1

        })

    }

}

}

我需要在打印“显示大图标”时调用该功能。

1 个答案:

答案 0 :(得分:0)

选项1。您可以在open类中添加完成块:

typealias VoidBlock = () -> Void
open class ItemAnnotationView: MKAnnotationView {
    var completionHandler: VoidBlock?
}

以及当您的图标变大时完成的调用:

// Start showing large icon
            self.largeIconView.alpha = 0.2
    //call completion 
    self.completionHandler?()

在您的控制器中,当您的open class allocate设置完成时:

//not sure how are you doing this, so the code below is just my thoughts 
let itemAnnotationView = ItemAnnotationView()
itemAnnotationView.completionHandler = {
    //do something here when the icon is large
}

选项2。第二种方法是使用委托

//define the protocol 
protocol ItemAnnotationViewDelegate {
    func iconDidBecomeLarge()
}

然后在open class中设置委托var:

open class ItemAnnotationView: MKAnnotationView{
    var delegate: ItemAnnotationViewDelegate?
}

然后在图标很大时调用委托方法:

// Start showing large icon

            self.largeIconView.alpha = 0.2
    //call the delegate method
self.delegate.iconDidBecomeLarge()

现在我们需要在分配开放类时从控制器设置委托:

let itemAnnotationView = ItemAnnotationView()
    itemAnnotationView.delegate = self

设置委托要求控制器符合新协议

您可以将其设为扩展名:

extension YourCointroller: ItemAnnotationViewDelegate {
    func iconDidBecomeLarge(){
        //your icon is large now, do additional stuff
    }
}