欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS实现侧滑栏效果

程序员文章站 2023-12-16 19:11:22
效果   源码:https://github.com/youxianming/ios-project-examples 中的 sideviewcontro...

效果

 iOS实现侧滑栏效果

源码: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

iOS实现侧滑栏效果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: