IOS drawer effect Demo, iosui
CAT/CAT sharing, must be excellent
Material address: http://blog.csdn.net/u013357243/article/details/45305785
For Original Articles, please reprint them. Reprinted Please note: Yan Nai-yu's blog
Http://blog.csdn.net/u013357243? Viewmode = contents
First view results
The first step is to set the three views and colors.
# Warning Step 1-(void) addChildView {// left UIView * leftView = [[UIView alloc] initWithFrame: self. view. bounds]; leftView. backgroundColor = [UIColor greenColor]; [self. view addSubview: leftView]; _ leftView = leftView; // right UIView * rightView = [[UIView alloc] initWithFrame: self. view. bounds]; rightView. backgroundColor = [UIColor blueColor]; [self. view addSubview: rightView]; _ rightView = rightView; // mainView UIView * mainView = [[UIView alloc] initWithFrame: self. view. bounds]; mainView. backgroundColor = [UIColor redColor]; [self. view addSubview: mainView]; _ mainView = mainView ;}
Step 2: Obtain the point, get the x value of the finger sliding offset, and then calculate the expected displacement based on the algorithm.
# Warning Step 2-(void) touchesMoved :( NSSet *) touches withEvent :( UIEvent *) event {// obtain UITouch object UITouch * touch = [touches anyObject]; // obtain the current CGPoint currentPoint = [touch locationInView: self. view]; // obtain the previous point CGPoint prePoint = [touch previuslocationinview: self. view]; // offset of the x axis: the number of CGFloat offsetX = currentPoint offset of x when the finger moves a little. x-prePoint. x; // set the frame _ mainView of the current main view. frame = [self getCurrentFrameWithOffsetX: offsetX]; _ isDraging = YES ;}
Step 3: Observe the frame change of _ mainView
# Warning Step 3: Observe the frame change of _ mainView // 2. listener/*** add an observer to _ mainView ** KeyPath: Listener frame attribute ** options: Listener new value change */[_ mainView addObserver: self forKeyPath: @ "frame" options: NSKeyValueObservingOptionNew context: nil];} // when the frame attribute of _ mainView changes, the system calls-(void) observeValueForKeyPath :( NSString *) keyPath ofObject :( id) object change :( NSDictionary *) change context :( void *) context {NSLog (@ "% @", NSStringFromCGRect (_ mainView. frame); if (_ mainView. frame. origin. x <0) {// move to the left // display the right _ rightView. hidden = NO; // hide the left _ leftView. hidden = YES;} else if (_ mainView. frame. origin. x> 0) {// move to the right // display the left _ rightView. hidden = YES; // hide the _ leftView on the right. hidden = NO ;}}
Step 4
# Warning Step 4: Calculate the frame and position reset of the current main view based on the offset of the X axis. # Define HMMaxY 60 // when the finger offset is a little bit, calculate the frame-(CGRect) getCurrentFrameWithOffsetX (CGFloat) offsetX {CGFloat screenW = [UIScreen mainScreen] of the current main view based on the X axis offset bounds. size. width; CGFloat screenH = [UIScreen mainScreen]. bounds. size. height; // obtain the y-axis offset. The value of CGFloat offsetY = offsetX * HMMaxY/screenW at each point of movement of the finger. CGFloat scale = (screenH-2 * offsetY)/screenH; if (_ mainView. frame. origin. x <0) {// slide to the left scale = (s) CreenH + 2 * offsetY)/screenH;} // obtain the previous frame CGRect frame = _ mainView. frame; frame. origin. x + = offsetX; frame. size. height = frame. size. height * scale; frame. size. width = frame. size. width * scale; frame. origin. y = (screenH-frame. size. height) * 0.5; return frame;} # define HMRTarget 250 # define HMLTarget-220/* _ mainView. frame. origin. x> screenW * 0.5 go to CGRectGetMaxX (_ mainView. frame) <screenW * 0.5 go to left-220 * // locate-(void) touchesEnded :( NSSet *) touches withEvent :( UIEvent *) event {// reset if (_ isDraging = NO & _ mainView. frame. origin. x! = 0) {[UIView animateWithDuration: 0.25 animations: ^ {_ mainView. frame = self. view. bounds;}];} CGFloat screenW = [UIScreen mainScreen]. bounds. size. width; CGFloat target = 0; if (_ mainView. frame. origin. x> screenW * 0.5) {// locate target = HMRTarget;} else if (CGRectGetMaxX (_ mainView. frame) <screenW * 0.5) {// locate target = HMLTarget on the left;} [UIView animateWithDuration: 0.25 animations: ^ {if (target) {// locate the offset CGFloat offsetX = target-_ mainView on the left or right. // obtain the offset of the X axis. frame. origin. x; // set the frame _ mainView of the current main view. frame = [self getCurrentFrameWithOffsetX: offsetX];} else {// restore _ mainView. frame = self. view. bounds ;}}]; _ isDraging = NO ;}
Ps: new iOS communication learning group: 304570962 can be added to cat qq: 1764541256 or znycat. Let's study hard together.
Yan Nai-yu's blog
Http://blog.csdn.net/u013357243? Viewmode = contents