Change label color using UITapGestureRecognizer

I have a label with UITapGestureRecognizer and longgestureRecognizer:

let gestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("labelPressed:"))
let longgestureRecognizer = UILongPressGestureRecognizer(target: self, action: Selector("longLabelPressed:"))
label.addGestureRecognizer(gestureRecognizer)
label.addGestureRecognizer(longgestureRecognizer)

I want to change the color, as in UIButton, when he clicked:

func longLabelPressed(recognizer:UILongPressGestureRecognizer){
        if let label = recognizer.view as? UILabel {
            if recognizer.state == .Began {
                label.textColor = UIColor.redColor()
            }

            if recognizer.state == .Ended {
                label.textColor = UIColor.blackColor()
            }



        }
    }

But how to determine the event of the end of the event?

func labelPressed(recognizer:UITapGestureRecognizer) {
        if let label = recognizer.view as? UILabel {

        }

    }

My goal is to create a shortcut like UIButton with touch events.

+4
source share
4 answers

UserInteractionEnabled for the default label is false . so if you use your label outlet, turn it on (XIB / storyboard)

, , , UILongPressGestureRecognizer , UILongPressGestureRecognizer, ( ). recognizer.state == .Began, Gelure

longgestureRecognizer.minimumPressDuration = 0.001

recognizer.state == .Began, .

+3

UILabel :

Objective-C

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    self.textColor = [UIColor blueColor];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    self.textColor = [UIColor whiteColor];
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    self.textColor = [UIColor blackColor];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    self.textColor = [UIColor redColor];
}

Swift 4

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    textColor = UIColor.blue
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    textColor = UIColor.white
}

override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
    textColor = UIColor.black
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    textColor = UIColor.red
}

:

Objective-C

label.userInteractionEnabled = YES;

Swift 4

label.isUserInteractionEnabled = true

xib :)

: UILabel + touchDown

+3

docs, .

let gestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("labelPressed:"))
let longgestureRecognizer = UILongPressGestureRecognizer(target: self, action: Selector("longLabelPressed:"))

label.userInteractionEnabled = YES;

label.addGestureRecognizer(gestureRecognizer)
label.addGestureRecognizer(longgestureRecognizer)
+1

:

  • userInteractionEnabled true ( )
  • Use UITapGestureRecognizerwill only provide you with information if the gesture is completed. If you want to know when it will start, I would suggest using UILongPressGestureRecognizerand setting the property minimumPressDurationto 0.
0
source

Source: https://habr.com/ru/post/1623269/


All Articles