IOS drawer effect

Time:2021-7-28

This example shares the specific code of IOS drawer effect for your reference. The specific contents are as follows

Drawer effect:

#import "DragerViewController.h"

#define screenW [UIScreen mainScreen].bounds.size.width

@interface DragerViewController ()

@property (nonatomic, weak) UIView *leftV;
@property (nonatomic, weak) UIView *rightV;
@property (nonatomic, weak) UIView *mainV;

@end

@implementation DragerViewController

- (void)viewDidLoad {
 [super viewDidLoad];

 //Add child controls
 [self setUp];


 //Add gesture
 UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];

 [self.mainV addGestureRecognizer:pan];


 //Add a tap gesture to the view of the controller
 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
 [self.view addGestureRecognizer:tap];
}

- (void)tap{
 //Reset mainv

 [UIView animateWithDuration:0.5 animations:^{
 self.mainV.frame = self.view.bounds;
 }];

}



#define targetR 275
#define targetL -275
- (void)pan:(UIPanGestureRecognizer *)pan{

 //Get offset
 CGPoint transP = [pan translationInView:self.mainV];
 //Why not use transform because we have to modify the height. Using transform, we can only modify X and y
 //self.mainV.transform = CGAffineTransformTranslate(self.mainV.transform, transP.x, 0);

 self.mainV.frame = [self frameWithOffsetX:transP.x];
 //Determine the direction of drag
 if(self.mainV.frame.origin.x > 0){
 //Right
 self.rightV.hidden = YES;
 }else if(self.mainV.frame.origin.x < 0){
 //Left
 self.rightV.hidden = NO;
 }

 //When the finger is released, do automatic positioning
 CGFloat target = 0;
 if (pan.state == UIGestureRecognizerStateEnded) {

 if (self.mainV.frame.origin.x > screenW * 0.5 ) {
  //1 judge on the right
  //Is the X of the current view greater than half of the screen width? If it is greater than, it is on the right
  target = targetR;
 }else if(CGRectGetMaxX(self.mainV.frame) < screenW * 0.5){
  //2. The judgment is on the left
  //Is the maximum X of the current view less than half the screen width? If it is less than, it is on the left
  target = targetL;
 }


 //Calculate the frame of the current mainv
 CGFloat offset = target - self.mainV.frame.origin.x;
 [UIView animateWithDuration:0.5 animations:^{

  self.mainV.frame = [self frameWithOffsetX:offset];
 }];


 }


 //Reset
 [pan setTranslation:CGPointZero inView:self.mainV];

}

#define maxY 100
//Calculate the frame of mainv according to the offset
- (CGRect)frameWithOffsetX:(CGFloat)offsetX {

 NSLog(@"offsetX===%f",offsetX);

 CGRect frame = self.mainV.frame;
 NSLog(@"x====%f",frame.origin.x);
 frame.origin.x += offsetX;

 //When the x value of the dragged view is equal to the screen width, maxy is the maximum, and the maximum is 100
 // 375 * 100 / 375 = 100

 //Take the absolute value of the calculated result
 CGFloat y = fabs( frame.origin.x * maxY / screenW);
 frame.origin.y = y;


 //Subtract twice the y value from the height of the screen
 frame.size.height = [UIScreen mainScreen].bounds.size.height - (2 * frame.origin.y);

 return frame;
}


- (void)setUp{

 //leftV
 UIView *leftV = [[UIView alloc] initWithFrame:self.view.bounds];
 leftV.backgroundColor = [UIColor blueColor];
 self.leftV = leftV;
 [self.view addSubview:leftV];
 //rightV
 UIView *rightV = [[UIView alloc] initWithFrame:self.view.bounds];
 rightV.backgroundColor = [UIColor greenColor];
 self.rightV = rightV;
 [self.view addSubview:rightV];
 //mianV
 UIView *mainV = [[UIView alloc] initWithFrame:self.view.bounds];
 mainV.backgroundColor = [UIColor redColor];
 self.mainV = mainV;
 [self.view addSubview:mainV];
}

@end

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.

Recommended Today

VBS obtains the operating system and its version number

VBS obtains the operating system and its version number ? 1 2 3 4 5 6 7 8 9 10 11 12 ‘************************************** ‘*by r05e ‘* operating system and its version number ‘************************************** strComputer = “.” Set objWMIService = GetObject(“winmgmts:” _  & “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”) Set colOperatingSystems = objWMIService.ExecQuery _  (“Select * from […]