How to highlight a UIView like Apple does

swift highlight button when pressed
uibutton change background color when pressed swift
uibutton custom highlight
uibutton flash when tapped
showstouchwhenhighlighted
swift change button color when pressed
swiftui touchdown
uibutton show touch on highlight

When the user touches a UIButton it gets grayed-out a bit. What apple does to get such an effect? I need the same effect on my custom UIButton when it is highlighted.

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    AppDelegate* appDelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
    if ([keyPath isEqualToString:@"highlighted"]){
        UIButton *button = object;
        if (button.isHighlighted) {
            self.backgroundColor=[UIColor colorWithRed:36.0/255.0 green:153.0/255.0 blue:116.0/255.0 alpha:1];
        }else{
            self.backgroundColor=appDelegate.currentAppColor;
        }
    }
}

Im using this code, but changing background color wont affect any subviews. I need them to be grayed out too.


I don't think there is an easy or definitive answer to that.

DISCLAIMER: All the code in this answer was written from the top of my head, so please excuse mistakes.

I would suggest doing something like this:

Proposal 1: decrease opacity for all the view's subviews, which would not affect color...

Objective C:

-(void)buttonTouched{
    for(UIView *subview in self.subviews){ 
        subview.alpha = 0.5;
    }
}

Swift:

func buttonTouched() {
    for subview in subviews {
        subview.alpha = 0.5
    }
}

Proposal 2 (not tested): try to cover all subviews by doing it kind of manually (drawback: you would have to set the colors back manually as well, which would be insane if it is not monochromatic):

Objective C:

-(void)buttonTouched{
    for(UIView *subview in self.subviews){

        if([subview respondsToSelector:@selector(setBackgroundColor:)]){ 
            //for generic views - changes UILabel's backgroundColor too, though
            subview.backgroundColor = [UIColor grayColor];
        }

        if([subview respondsToSelector:@selector(setTextColor:)]){ 
            //reverse effect of upper if statement(if needed)
            subview.backgroundColor = [UIColor clearColor];
            subview.textColor = [UIColor grayColor];
        }
    }
}

Swift:

func buttonTouched() {
    for subview in subviews {
        if subview.responds(to: #selector(setter: AVMutableVideoCompositionInstruction.backgroundColor)) {
            //for generic views - changes UILabel's backgroundColor too, though
            subview.backgroundColor = UIColor.gray
        }

        if subview.responds(to: #selector(setter: UILabel.textColor)) {
            //reverse effect of upper if statement(if needed)
            subview.backgroundColor = UIColor.clear
            subview.textColor = UIColor.gray
        }
    }
}

This is really bad design and will probably cause a lot of problems, but it could potentially help you. The example above needs a lot of improvement, I just want to give you a hint. You would have to revert the colors in the touchesEnded: method. Maybe it helps you...

Proposal 3: would be to overlay your whole view with a transparent view (if it is rectangular)

Objective C:

-(void)buttonTouched{
    UIView *overlay = [[UIView alloc]initWithFrame:self.bounds];
    overlay.backgroundColor = [[UIColor grayColor]colorWithAlphaComponent:0.5];
    [self addSubview: overlay];
}

Swift:

func buttonTouched() {
    let overlay = UIView(frame: bounds)
    overlay.backgroundColor = UIColor.gray.withAlphaComponent(0.5)
    addSubview(overlay)
}

You would have to remove it when the user releases the finger of course.

Proposal 4 : Another option would be to create a bitmap of your current view and modify the pixels to your liking, which is quite a bit of work, so I will omit code here.

Apple probably does a mix of the last two. When a button is touched it will go over the pixels and overlay a gray pixel over every pixel that has an alpha component.

I hope I could help.

isHighlighted, You can change the value of this property as needed to apply or remove a highlight programmatically. The default value of this property is false for a newly  The UIStack View is a nonrendering subclass of UIView; that is, it does not provide any user interface of its own. Instead, it just manages the position and size of its arranged views. As a result, some properties (like background Color) have no effect on the stack view.


You can use a custom view like this for example:

class HighlightView: UIView {

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        DispatchQueue.main.async {
            self.alpha = 1.0
            UIView.animate(withDuration: 0.4, delay: 0.0, options: .curveLinear, animations: {
                self.alpha = 0.5
            }, completion: nil)
        }
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        DispatchQueue.main.async {
            self.alpha = 0.5
            UIView.animate(withDuration: 0.4, delay: 0.0, options: .curveLinear, animations: {
                self.alpha = 1.0
            }, completion: nil)
        }
    }

    override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
        DispatchQueue.main.async {
            self.alpha = 0.5
            UIView.animate(withDuration: 0.4, delay: 0.0, options: .curveLinear, animations: {
                self.alpha = 1.0
            }, completion: nil)
        }
    }
}

Then you can use it instead of UIView and whenever you will click on it, it will change it's alpha value so it will look like a highlight.

Changing the Appearance of Selected and Highlighted Cells, As a user taps a cell, the app determines the state of the cell and changes the cell's The collection view in this sample supports single-item selection, which is the default for awakeFromNib() let redView = UIView(frame: bounds) redView. I have some years of experience in development for the best mobile platform — iOS. During this time I’ve seen many iOS apps and many iOS engineers. Our world is full of good developers, but


I know this is an old question. But if someone is still searching for a generic touch highlighting solution, here is a little category I have created to address the problem.

https://github.com/mta452/UIView-TouchHighlighting

Usage:

Just import the category in your view controller and add the following line in viewDidLoad.

buttonView.touchHighlightingStyle = MTHighlightingStyleHollowDarkOverlay;

Different Highlighting Styles:

backgroundColor, Changes to this property can be animated. The default value is nil , which results in a transparent background color. var tintAdjustmentMode: UIView. To do this, open Main.storyboard and in the document outline, highlight both UIViews by Command + clicking both of them. Then go to Editor/Embed In/View . You should now see a white view that’s slightly larger than the other two.


Swift 3:

override touchesBegan and touchesEnded callbacks in ur custom UIView class

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.layer.backgroundColor = UIColor.white.cgColor
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.layer.backgroundColor = UIColor.gray.cgColor
}

or better use drop shadow instead of changing background

CardView code except touches event borrowed from https://github.com/aclissold/CardView

@IBDesignable
class CardView: UIView {

    @IBInspectable var cornerRadius: CGFloat = 5

    @IBInspectable var shadowOffsetWidth: Int = 0
    @IBInspectable var shadowOffsetHeight: Int = 1
    @IBInspectable var shadowColor: UIColor? = UIColor.black
    @IBInspectable var shadowOpacity: Float = 0.23

    override func layoutSubviews() {
        layer.cornerRadius = cornerRadius
        let shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius)

        layer.masksToBounds = false
        layer.shadowColor = shadowColor?.cgColor
        layer.shadowOffset = CGSize(width: shadowOffsetWidth, height: shadowOffsetHeight);
        layer.shadowOpacity = shadowOpacity
        layer.shadowPath = shadowPath.cgPath
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        layer.shadowOffset = CGSize(width: shadowOffsetWidth, height: shadowOffsetHeight * 2);

    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        layer.shadowOffset = CGSize(width: shadowOffsetWidth, height: 

    shadowOffsetHeight);
        }

    }

UPDATE:

I also added touchesCancelled callback to prevent my UIView stuck in selected state.

 override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
        layer.shadowOffset = CGSize(width: shadowOffsetWidth, height: shadowOffsetHeight);
    } 

UIButton, Buttons have five states that define their appearance: default, highlighted, focused, You can specify a button's title as a plain string or attributed string. about the button's inherited Interface Builder attributes, see UIControl and UIView . How to highlight text on an Apple iPhone and iPad. Press and hold down on any text with your finger, drag your finger over the text you'd like to highlight, and then let go. Upon letting go, a menu should appear. Press your finger on the Select option.


Ended up with this code

maskView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
maskView.backgroundColor=[[UIColor blackColor]colorWithAlphaComponent:0.5];

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
        if ([keyPath isEqualToString:@"highlighted"]){
            UIButton *button = object;
            if (button.isHighlighted) {
                [self addSubview:maskView];
            }else{
                [maskView removeFromSuperview];
            }
        }
    }

How to set the tint color of a UIView, tintColor can be set for any individual view to color just one view, If you want to tint all views in your app, put this in your AppDelegate.swift: So vinegar, like raw apple cider vinegar, will leave reddish highlights in the same hair color. To start, mix apple cider vinegar 50/50 with distilled water. To start, mix apple cider vinegar 50/50 with distilled water.


How to give UITableViewCells a selected color other than gray, let backgroundView = UIView() backgroundView. You can customize the view as much as you want to, but remember to keep the overall  Storyboard no more. Crafting (coding) a custom UIView. If you right click on it and then select open as Source Code you’ll have something like this: We can do that like apple does and


Working with iOS View Animations and Transitions in Swift, If you use an Apple device, you've probably seen a bunch of animations from Animations also highlight changes on the screen and can help a user learn to navigate your app. You should find a file structure that looks like this: In our transition() action method, if you start to type UIView.transition , the  What are the standard words for the "A as in Apple", "B as in Boy" code? Is there a certain name to the "a as in apple", "b as in boy" code? I tried looking it up but had a hard time getting results. I'm curious as to what all the standard words are for the alphabet.


Sams Teach Yourself iPhone Application Development in 24 Hours, This will serve as the background image for the application. Select apple.png from the Image drop-down list. 3. Next, drag a UIView on top of the image view. A picker view displays one or more wheels that the user manipulates to select items. Each wheel—known as a component—has a series of indexed rows representing the selectable items. Each row displays a string or view so that the user can identify the item on that row.