Shadowing effect of high brightness with ios

Keywords: iOS

ios UI controls usually add highlighting effects when highlighting is selected to prompt users of the currently selected view and improve user experience.

Both UIButton and UITableViewCell systems have API s for setting shadows when highlighted, which I won't go into much detail here. Things like UIImageView, UIView, or card-like controls that combine these controls. These controls can achieve the same effect as buttons by adding UITap Gesture Recognizer, which requires adding highlights when selected on these controls. There are two ways to achieve this effect:

1. Add a view that can change the background color on these spaces, and then listen to his touch Begin/end/cancel mode to change the background color. The code is as follows:

// Add background color to start touching
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    self.backgroundColor = [UIColor colorWithHexString:@"f8f8f8"];
}
// Clear the background color at the end
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    if (self.shouldShowSelectedBg) {
       self.backgroundColor = [UIColor clearColor];
    }
}
// Clear the background color when the gesture moves
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesCancelled:touches withEvent:event];
    if (self.shouldShowSelectedBg) {
       self.backgroundColor = [UIColor clearColor];
    }
}

Since a variable color background view is added separately, the frame of the shadow view at highlight can be controlled by controlling the frame of the view.

2. When touchesBegin mode of view is triggered, a shaded view is inserted at the bottom of the current view and removed when touchesEnded/cancel. The concrete realization is as follows:

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    
    if (!self.selectedBgView) {
        UIView *v = [[UIView alloc] initWithFrame:self.shadowView.bounds];
        self.selectedBgView = v;
        v.backgroundColor = [UIColor colorWithHexString:@"f8f8f8"];
    }
    [self.bgView insertSubview:self.selectedBgView atIndex:0];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    [self.selectedBgView removeFromSuperview];
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesCancelled:touches withEvent:event];
    [self.selectedBgView removeFromSuperview];
}

There are advantages and disadvantages in comparing the two methods.

Method 1 needs to add an additional view to the view to increase memory, but it can be inherited by multiple views. It can reduce code redundancy by not writing touchesBegin/end/cancel method once for each view.

Method 2 can reduce memory usage by adding view only when highlighting is selected, but if there are multiple views to add highlighting effect, it is necessary to write similar code once in each view to increase code redundancy.

Fish and bear's paw can't be used at the same time. We can use that method according to the situation.

Note: 1. The background view cannot be removed in the touchesMoved method, otherwise the background view will be removed immediately after selection.

2. Background view is inserted at the bottom, not covered at the top.

Posted by Goose87 on Sat, 22 Jun 2019 13:46:04 -0700