Jerome
Jerome

Reputation: 2152

can not update UImageView's image in Custom UIview

it's my first time to ask question, if there's anything wrong, please tell me softly.:]

Here's my custom UIView, there's a UIImageView in it. I set its image and highlighted image. I wish i can change them when I pan on it. But it could not update image.

Thank you for reading, hope you can help me and others who facing the same problem like me.

import Foundation
import UIKit

class TextStickerView: UIView {
    var defaultInset: CGFloat = 20
    var defaultMinimumSize: CGFloat {
        return defaultInset * 4
    }
    let buttonSize = CGSize(width: 45, height: 45)
    // Variables for moving view
    var beginPoint: CGPoint!
    var beginCenter: CGPoint!

    var moveBtn: UIImageView {
        let myView = UIImageView(frame: CGRect(origin: CGPoint(x: bottomLeftImageView.frame.origin.x + defaultInset / 3, y: CGRectGetMaxY(bottomLeftImageView.frame)), size: buttonSize))
        myView.image = UIImage(named: "text_move")
        myView.highlightedImage = UIImage(named: "text_move_down")
        myView.contentMode = .ScaleToFill
        let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture(_:)))
        myView.addGestureRecognizer(panGesture)
        myView.userInteractionEnabled = true
        return myView
    }
    func handlePanGesture(recognizer: UIGestureRecognizer) {
        print("pan")
        let touchLocation = recognizer.locationInView(self.superview)
        switch recognizer.state {
            case .Began:
                moveBtn.highlighted = true
                beginPoint = touchLocation
                beginCenter = self.center
                /// FIX: can not update moveBtn's image after set moveBtn.highlighted true
                setNeedsDisplay()
            case .Changed:
                self.center = CGPoint(x: beginCenter.x + (touchLocation.x - beginPoint.x), y: beginCenter.y + (touchLocation.y - beginPoint.y))
            case .Ended:
                self.center = CGPoint(x: beginCenter.x + (touchLocation.x - beginPoint.x), y: beginCenter.y + (touchLocation.y - beginPoint.y))
                moveBtn.highlighted = false
                setNeedsDisplay()


            default:
                break
        }
        init(contentView: UIView) {
        self.contentView = contentView
        self.contentView.frame.origin = CGPoint(x: defaultInset, y: defaultInset)
        let frame = CGRect(x: 0, y: 0, width: contentView.frame.width + defaultInset * 2, height: contentView.frame.height + defaultInset * 2 + buttonSize.height)

        super.init(frame: frame)

        self.addSubview(self.moveBtn)
        self.moveBtn.userInteractionEnabled = true
    }
}

Upvotes: 2

Views: 170

Answers (1)

Lumialxk
Lumialxk

Reputation: 6369

var moveBtn: UIImageView { is a computing property. So every time you get it, you will get a new image view. Try this:

    var moveBtn: UIImageView = {
        let myView = UIImageView(frame: CGRect(origin: CGPoint(x: bottomLeftImageView.frame.origin.x + defaultInset / 3, y: CGRectGetMaxY(bottomLeftImageView.frame)), size: buttonSize))
        myView.image = UIImage(named: "text_move")
        myView.highlightedImage = UIImage(named: "text_move_down")
        myView.contentMode = .ScaleToFill
        let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture(_:)))
        myView.addGestureRecognizer(panGesture)
        myView.userInteractionEnabled = true
        return myView
    }()

Upvotes: 1

Related Questions