iOS实现侧滑栏效果
程序员文章站
2023-12-13 08:17:52
效果
源码:https://github.com/youxianming/ios-project-examples 中的 sideviewcontro...
效果
源码:https://github.com/youxianming/ios-project-examples 中的 sideviewcontroller
// // viewcontroller.m // sideviewcontroller // // created by youxianming on 16/6/6. // copyright © 2016年 youxianming. all rights reserved. // #import "viewcontroller.h" #import "leftviewcontroller.h" #import "mainviewcontroller.h" #import "uiview+setrect.h" @interface viewcontroller () { cgfloat _screenwidth; } @property (nonatomic, strong) uipangesturerecognizer *pangesture; @property (nonatomic) cgpoint panbeginpoint; @property (nonatomic, strong) leftviewcontroller *leftviewcontroller; @property (nonatomic, strong) uiview *leftview; @property (nonatomic, strong) mainviewcontroller *mainviewcontroller; @property (nonatomic, strong) uiview *mainview; @end @implementation viewcontroller - (void)viewdidload { [super viewdidload]; // init some value. _screenwidth = width; // add backgroundview. uiimageview *backgroundview = [[uiimageview alloc] initwithframe:self.view.bounds]; backgroundview.image = [uiimage imagenamed:@"back"]; [self.view addsubview:backgroundview]; // leftviewcontroller self.leftviewcontroller = [[leftviewcontroller alloc] init]; self.leftview = self.leftviewcontroller.view; [self.view addsubview:self.leftview]; // mainviewcontroller self.mainviewcontroller = [[mainviewcontroller alloc] init]; self.mainview = self.mainviewcontroller.view; [self.view addsubview:self.mainview]; // pan gesture. self.pangesture = [[uipangesturerecognizer alloc] initwithtarget:self action:@selector(pangestureevent:)]; [self.mainview addgesturerecognizer:self.pangesture]; } - (void)pangestureevent:(uipangesturerecognizer *)gesture { cgpoint translation = [gesture translationinview:gesture.view]; cgpoint velocity = [gesture velocityinview:gesture.view]; cgfloat gap = _screenwidth / 3.f * 2; cgfloat sensitiveposition = _screenwidth / 2.f; if (velocity.x < 0 && _mainview.x <= 0) { // 过滤掉向左侧滑过头的情形 _mainview.x = 0.f; } else { if (gesture.state == uigesturerecognizerstatebegan) { // 开始 _panbeginpoint = translation; if (_mainview.x >= sensitiveposition) { _panbeginpoint.x -= gap; } } else if (gesture.state == uigesturerecognizerstatechanged) { // 值变化 _mainview.x = translation.x - _panbeginpoint.x; if (_mainview.x <= 0) { // 过滤掉向左侧滑过头的情形 _mainview.x = 0.f; } } else if (gesture.state == uigesturerecognizerstateended) { // 结束 [uiview animatewithduration:0.20f animations:^{ _mainview.x >= sensitiveposition ? (_mainview.x = gap) : (_mainview.x = 0); }]; } } } @end
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。