0

I have a dynamically created list of labels and I am using GestureRecognizer for each of the labels to detect touches/click on them. I couldn't use UIButton as I wanted to pass some text which is unique to each label to the touch event handler and UIButton would not allow text to be passed as userinfo. I cannot use UIButton.tag to pass the additional info.

Now I want the UIButton like glow effect on touch on my UIlabel. If there are other ways to notify a user that a label was touched, that work too. I was alo thinking of using some kind of quick animation or jiggling effect. Any ideas or workarounds?

Thanks in advance.

chown
  • 51,908
  • 16
  • 134
  • 170
apy
  • 67
  • 2
  • 6

2 Answers2

0

Swift 4

1- Create UIView extension with the animation

2- Use it on textfields, buttons, views (any subclass of UIView)

UIView extension

import UIKit

extension UIView{
    enum GlowEffect:Float{
        case small = 0.4, normal = 1, big = 5
    }

    func doGlowAnimation(withColor color:UIColor, withEffect effect:GlowEffect = .normal) {
        layer.masksToBounds = false
        layer.shadowColor = color.cgColor
        layer.shadowRadius = 0
        layer.shadowOpacity = effect.rawValue
        layer.shadowOffset = .zero

        let glowAnimation = CABasicAnimation(keyPath: "shadowRadius")
        glowAnimation.fromValue = 0
        glowAnimation.toValue = 1
        glowAnimation.beginTime = CACurrentMediaTime()+0.3
        glowAnimation.duration = CFTimeInterval(0.3)
        glowAnimation.fillMode = kCAFillModeRemoved
        glowAnimation.autoreverses = true
        glowAnimation.isRemovedOnCompletion = true
        layer.add(glowAnimation, forKey: "shadowGlowingAnimation")
    }
}

How to use it:

//Button
button.doGlowAnimation(withColor: UIColor.red, withEffect: .big)
alegelos
  • 2,308
  • 17
  • 26
0

Why dont you create buttons programmatically and make the button type as custom. Like so -

UIButton *sampleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[sampleButton setBackgroundImage:[[UIImage imageNamed:@"redButton.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
[sampleButton setTitle:@"My Button"];
[sampleButton addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:sampleButton];

Here we have defined what the button should look like UIControlStateNormal. You can define what it should look like for UIControlStateHighlighted.

What i want to say is that using uibuttons you can get what you need. Dont have to use uilabels.

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
  • I have used UIButton's now instead of UILabel. Since I couldn't pass extra parameter with the addTarget message for UIButton, I now maintain a map of UIButton.tag to the additional information I need when a button is pressed. Seems to work for now and I can use all the functionality of UIButton. – apy Oct 17 '11 at 11:38
  • ok, but if you want a uilabel to behave like a uibutton. then it's better to use a uibutton! Anyway as long as it works... Glad i could share what i knew... – Srikar Appalaraju Oct 17 '11 at 12:09