English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Introducción
La implementación simple del efecto de gabinete en iOS se utiliza en muchos aplicaciones en la actualidad, hay muchos ejemplos en línea, este artículo principalmente a través de algunos códigos simples para implementar, si necesitan pueden aprender juntos.
A continuación, se muestra la imagen del efecto
Ejemplo de código a continuación
#import <UIKit/UIKit.h> @interface MainViewController : UIViewController + (instancetype)mainViewControllerWithLeftViewController:(UIViewController *)leftViewController withMainPageViewController:(UIViewController *)mainPageViewController; @end
#import "MainViewController.h" #define KWidth self.view.frame.size.width #define KHeight self.view.frame.size.height @interface MainViewController () @property (nonatomic,strong)UIViewController *leftVC; @property (nonatomic,strong)UIViewController *centerVC; @property (nonatomic,assign)BOOL isSlider; @property (nonatomic,strong)UIView *corverView; @end @implementation MainViewController + (instancetype)mainViewControllerWithLeftViewController:(UIViewController *)leftViewController withMainPageViewController:(UIViewController *)mainPageViewController{ MainViewController *mainVC = [[MainViewController alloc] init]; mainVC.leftVC = leftViewController; mainVC.centerVC = mainPageViewController; return mainVC; } - (void)viewDidLoad{ [super viewDidLoad]; self.isSlider = NO; self.view.backgroundColor = [UIColor whiteColor]; [self addChildViewController:self.leftVC]; [self.view addSubview:self.leftVC.view]; [self addChildViewController:self.centerVC]; [self.view addSubview:self.centerVC.view]; // Agregar gestos a la vista izquierda y la vista principal [self addGestureForView]; } // Agregar cubierta a la vista principal - (void)addCorverView{ if (self.corverView) { [self.corverView removeFromSuperview]; self.corverView = nil; } self.corverView = [[UIView alloc] initWithFrame:self.centerVC.view.bounds]; _corverView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0]; [self.centerVC.view addSubview:self.corverView]; } // Remover la cubierta de la vista principal - (void)removeCoverView{ if (self.corverView) { [self.corverView removeFromSuperview]; self.corverView = nil; } } // Agregar gestos a la vista izquierda y la vista principal - (void)addGestureForView{ UISwipeGestureRecognizer *rightGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRightAction:)]; rightGesture.direction = UISwipeGestureRecognizerDirectionRight; [self.centerVC.view addGestureRecognizer:rightGesture]; UISwipeGestureRecognizer *leftGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeftAction:)]; leftGesture.direction = UISwipeGestureRecognizerDirectionLeft; [self.centerVC.view addGestureRecognizer:leftGesture]; UISwipeGestureRecognizer *leftVCLeftSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftVCLeftSwipeAction:)]; leftVCLeftSwipeGesture.direction = UISwipeGestureRecognizerDirectionLeft; [self.leftVC.view addGestureRecognizer:leftVCLeftSwipeGesture]; } - (void)swipeRightAction:(id)sender{ [self moveView:self.centerVC.view scale:0.8 panValue:KWidth]; self.isSlider = YES; [self addCorverView]; } - (void)swipeLeftAction:(id)sender{ [self moveView:self.centerVC.view scale:1 panValue:KWidth / 2]; self.isSlider = NO; [self removeCoverView]; } - (void)leftVCLeftSwipeAction:(id)sender{ [self moveView:self.centerVC.view scale:1 panValue:KWidth / 2]; self.isSlider = NO; [self removeCoverView]; } // Mover y escalar una vista - (void)moveView:(UIView *)view scale:(CGFloat)scale panValue:(CGFloat)value{ [UIView beginAnimations:nil context:nil]; view.transform = CGAffineTransformScale(CGAffineTransformIdentity,scale,scale); view.center = CGPointMake(value, view.center.y); [UIView commitAnimations]; } @end
Resumen
Esto es todo el contenido de este artículo. Espero que el contenido de este artículo pueda ayudar a sus estudios o trabajo. Si tienen alguna pregunta, pueden dejar comentarios para intercambiar.
Declaración: El contenido de este artículo se ha obtenido de la red, es propiedad del autor original, el contenido se ha contribuido y subido por los usuarios de Internet de manera autónoma. Este sitio no posee los derechos de propiedad, no ha sido editado por humanos y no asume ninguna responsabilidad legal relacionada. Si encuentra contenido sospechoso de infracción de derechos de autor, por favor envíe un correo electrónico a: notice#oldtoolbag.com (al enviar un correo electrónico, por favor reemplace # con @) para denunciar, y proporcione evidencia relevante. Una vez confirmado, este sitio eliminará inmediatamente el contenido sospechoso de infracción.