Objective-C - How to use UIView on ios develop

tag: Objective-C    date: 2018-10-10

create a new uiview for test

UIView *view = [[UIView alloc] initWithFrame:(CGRect){110,100,100,100}];
view.backgroundColor = [UIColor redColor];
[self.view addSubview:view];
create uiview

uiview set alpha value

[redView setAlpha:0.5f];

uiview set border and border color

redView.layer.borderWidth = 4.0f;
redView.layer.borderColor = [UIColor blueColor].CGColor;
uiview set border

uiview set rounded corners - border radius

redView.layer.cornerRadius = 8.0f;
redView.layer.masksToBounds = YES;
uiview set rounded corners

uiview set shadow

redView.layer.shadowRadius = 4.0f;
redView.layer.shadowColor = [UIColor blackColor].CGColor;
redView.layer.shadowOffset = CGSizeMake(0.0f, 1.0f);
redView.layer.shadowOpacity = 0.6f;
redView.layer.masksToBounds = NO;
uiview set shadow

uiview set gradient background color

CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = redView.bounds;
gradient.colors = @[(id)[UIColor whiteColor].CGColor, (id)[UIColor redColor].CGColor];
[redView.layer insertSublayer:gradient atIndex:0];
uiview set gradient

uiview set background image

redView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"test-image.png"]];
uiview set background image

uiview set click gesture

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[redView addGestureRecognizer:tap];
-(void) tap:(UITapGestureRecognizer*)gesture{
    [[[UIAlertView alloc] initWithTitle:@"demo"
                                message:@"UIView Tap Press"
                               delegate:self
                      cancelButtonTitle:@"cancel"
                      otherButtonTitles:nil] show];
}
uiview set click gesture

uiview set long press gesture

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[redView addGestureRecognizer:longPress];
-(void) longPress:(UILongPressGestureRecognizer*)gesture{
    if ( gesture.state == UIGestureRecognizerStateEnded ) {
        [[[UIAlertView alloc] initWithTitle:@"demo"
                                    message:@"UIView Long Press"
                                   delegate:self
                          cancelButtonTitle:@"cancel"
                          otherButtonTitles:nil] show];
    }
}
uiview set long press gesture

uiview get parent UIViewController

- (UIViewController *)parentViewController:(UIView*) senderView {
    UIResponder *responder = senderView.nextResponder;
    while ([responder isKindOfClass:[UIView class]]) {
        responder = [responder nextResponder];
    }
    NSLog([responder description]);
    return (UIViewController *)responder;
}
Here is the output
2018-10-10 14:39:43.969 Demo[3380:150025] <ViewController: 0x7fafc5c07080>

uiview snapshot - convert uiview to uiimage

-(UIImage*) snapshot:(UIView*) view {
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0f);
    [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:NO];
    UIImage *snapshot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return snapshot;
}

uiview zoom in / zoom out animation

CABasicAnimation *zoomAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
zoomAnimation.duration = 1.0f;
zoomAnimation.toValue = [NSNumber numberWithFloat:1.5];
zoomAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
zoomAnimation.autoreverses = YES;
zoomAnimation.repeatCount = 20;
[view.layer addAnimation:zoomAnimation forKey:@"zoom"];
uiview zoom animation

uiview shake animation

CAKeyframeAnimation *shakeAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.x"];
shakeAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
shakeAnimation.duration = 0.5f;
shakeAnimation.values = @[@(-12), @(12), @(-8), @(8), @(-4), @(4), @(0)];
shakeAnimation.repeatCount = 100;
[view.layer addAnimation:shakeAnimation forKey:@"shake"];
uiview shake animation

uiview rotate animation

CABasicAnimation *rotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotateAnimation.toValue = @(M_PI * 2.0f);
rotateAnimation.duration = 1.0f;
rotateAnimation.autoreverses = YES;
rotateAnimation.repeatCount = 100;
rotateAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[view.layer addAnimation:rotateAnimation forKey:@"rotate"];
uiview rotate animation

uiview flip animation

CATransition *transition = [CATransition animation];
transition.startProgress = 0.0f;
transition.endProgress = 1.0f;
transition.type = @"flip";
transition.subtype = @"fromLeft";
transition.duration = 1.0f;
transition.repeatCount = 100;
transition.autoreverses = NO;
[redView.layer addAnimation:transition forKey:@"spin"];
uiview flip animation

uiview set fade in animation

[redView setAlpha:0.0f];
[UIView animateWithDuration:2.0f animations:^{
    [redView setAlpha:1.0f];
} completion:^(BOOL finished) {
    
}];
uiview set fade in animation

uiview set fade out animation

[redView setAlpha:1.0f];
[UIView animateWithDuration:2.0f animations:^{
    [redView setAlpha:0.0f];
} completion:^(BOOL finished) {
    
}];

more articles
Copyright © 2018
This Site is Licensed under a Creative Commons License. Powerd by Java