Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

This is wrong with this code?

dickthedevdickthedev Posts: 214Registered Users @ @
Hello, I am doing this as an Swift 3 function exercise. I have this code which allows two kinds of views to pain around on the iPhone. a UIView and a UIImageView. These codes work rather well.

@IBAction func myView(_ sender: UIPanGestureRecognizer) {
        let myView = sender.view!
        let point = sender.translation(in: myView)
        myView.center = CGPoint(x: view.center.x + point.x, y: view.center.y + point.y)
        if sender.state == UIGestureRecognizerState.ended {
            UIView.animate(withDuration: 0.2, animations: {
                myView.center = self.view.center
            })
        }
    }

    @IBAction func imgView(_ sender: UIPanGestureRecognizer) {
        myFunc(sender: <#T##UIPanGestureRecognizer#>)
        let imgView = sender.view!
        let point = sender.translation(in: imgView)
        imgView.center = CGPoint(x: view.center.x + point.x, y: view.center.y + point.y)
        if sender.state == UIGestureRecognizerState.ended {
            UIView.animate(withDuration: 0.2, animations: {
                imgView.center = self.view.center
            })
        }
    }

But the two @IBAction fouctions looks almost identical. So I like to change it into a function both @IBActions can use, so I created this function:
func myFunc(sender: UIPanGestureRecognizer) {
        let theView = sender.view!
        let point = sender.translation(in: theView)
        theView.center = CGPoint(x: view.center.x + point.x, y: view.center.y + point.y)
}

But when I tried to call it like this:
super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

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

@IBAction func myView(_ sender: UIPanGestureRecognizer) {
        myFunc(sender: UIPanGestureRecognizer)
    }


    @IBAction func imgView(_ sender: UIPanGestureRecognizer) {
        myFunc(sender: UIPanGestureRecognizer)
    }

    // Need to crate a function to handle all views.
    func myFunc(sender: UIPanGestureRecognizer) {
        let theView = sender.view!
        let point = sender.translation(in: theView)
        theView.center = CGPoint(x: view.center.x + point.x, y: view.center.y + point.y)
    }
}

I instantly get Error in Xcode "Editor placeholder in source", what am I doing wrong? And what does "Editor placeholder in source" mean?

Thanks

Replies

  • Duncan CDuncan C Posts: 9,114Tutorial Authors, Registered Users @ @ @ @ @ @ @
    It says there is a placeholder in your code. That means you copy/pasted it from a template without replacing the template with your code.

    Your IBAction should read:
    @IBAction func imgView(_ sender: UIPanGestureRecognizer) {
            myFunc(sender: sender)
    }
    
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
Sign In or Register to comment.