iOS实现侧滑栏效果

时间:2022-09-19 16:27:02

效果

 iOS实现侧滑栏效果

源码:https://github.com/youxianming/ios-project-examples 中的 sideviewcontroller 

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//
// 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实现侧滑栏效果

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