按钮中的序列事件

时间:2018-02-13 21:03:44

标签: ios swift button action sequence

正下方我有2个按钮。需要在magicjohnson按钮之前按下放置按钮,否则会出现运行时错误。

    @IBAction func magicJohnson(_ sender: Any) {
        let photo = self.imageV.image
        let data = UIImagePNGRepresentation(photo!)
        if cdHandler.saveObject(pic:  data!){
        }}

    @IBAction func place(_ sender: Any) {
        let image = UIImage.imageWithView(view: theView)
        let heart:UIImage = UIImage(named: "words")!
        let left:UIImage = image
        let newSize2 = CGSize(width: left.size.width, height: left.size.height)
        UIGraphicsBeginImageContextWithOptions(newSize2, false, left.scale)
        left.draw(in: CGRect(x: 0,y: 0,width: newSize2.width,height:   newSize2.height), blendMode:CGBlendMode.normal, alpha:1.0)
        heart.draw(in: CGRect(x: 0,y: 0,width: newSize2.width,height:   newSize2.height), blendMode:CGBlendMode.normal, alpha:1.0)
        let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        imageV.image = newImage
    }

如何让代码基本上执行此操作/

 @IBAction func magicJohnson(_ sender: Any) {
     FIRST {
   let image = UIImage.imageWithView(view: theView)
        let heart:UIImage = UIImage(named: "words")!
        let left:UIImage = image
        let newSize2 = CGSize(width: left.size.width, height: left.size.height)
        UIGraphicsBeginImageContextWithOptions(newSize2, false, left.scale)
        left.draw(in: CGRect(x: 0,y: 0,width: newSize2.width,height:   newSize2.height), blendMode:CGBlendMode.normal, alpha:1.0)
        heart.draw(in: CGRect(x: 0,y: 0,width: newSize2.width,height:   newSize2.height), blendMode:CGBlendMode.normal, alpha:1.0)
        let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        imageV.image = newImage
            }
second{
        let photo = self.imageV.image
        let data = UIImagePNGRepresentation(photo!)
        if cdHandler.saveObject(pic:  data!){
        }
        }}

1 个答案:

答案 0 :(得分:1)

试试这个,将placeBu声明为IBOutlet

@IBAction func magicJohnson(_ sender: Any) {
    self.place(self.placeBu) // may be self.place(UIButton()) if you don't care of sender instead of the outlet
    let photo = self.imageV.image
    let data = UIImagePNGRepresentation(photo!)
    if cdHandler.saveObject(pic:  data!){
    }}

或者

@IBAction func magicJohnson(_ sender: Any) {

    if let photo = self.imageV.image
    { 
        let data = UIImagePNGRepresentation(photo!)
           if cdHandler.saveObject(pic:  data!){
        }}

    }
  }

在为magicJohnson按钮启用的接口构建器集中的OR为false

@IBAction func place(_ sender: Any) {
    let image = UIImage.imageWithView(view: theView)
    let heart:UIImage = UIImage(named: "words")!
    let left:UIImage = image
    let newSize2 = CGSize(width: left.size.width, height: left.size.height)
    UIGraphicsBeginImageContextWithOptions(newSize2, false, left.scale)
    left.draw(in: CGRect(x: 0,y: 0,width: newSize2.width,height:   newSize2.height), blendMode:CGBlendMode.normal, alpha:1.0)
    heart.draw(in: CGRect(x: 0,y: 0,width: newSize2.width,height:   newSize2.height), blendMode:CGBlendMode.normal, alpha:1.0)
    let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()
    imageV.image = newImage
    self.magicJohnson.isEnabled = true
}