如何在 ios objective c 中使用 uigesturerecognizer 滑动自定义五个子视图
How can swipe custom five sub views using uigesture recoginzier in ios objective c
我正在使用 *UISwipeGesture Recognizer,但现在只能使用两个视图,我们可以左右进行操作,但现在需要五个 UIview 才能显示为滑动,请帮帮我?
我只需要以下两个方向:
@左方向。
@正确的方向。
我想在我的视图中向左和向右滑动五个视图。
您可以通过iOS中的方法实现。
- 使用
UIScrollView
添加你想滑动的视图然后设置contentSize
,然后设置enablePaging
= YES,就完成了。
- 如果您的视图属于视图控制器,您可以使用
UIPageViewController
并添加 set viewControllers 属性 并设置滚动方向(垂直或水平)并完成。
如果你想通过 SwipeGestureRecognizer
处理它,你仍然可以这样做,在视图中添加手势识别器,然后将你想要滑动的所有视图作为子视图添加到其中,在向左和向右滑动时,您可以通过链接其框架将下一个或上一个视图放在前面,以跟踪哪个应该是当前可见视图以及上一个和下一个视图是什么,您可以保留一个适当的整数并在开始时将其值设置为 0 ,向左滑动递增 1,向右滑动递减,它应该始终为 0<=index<5.
编辑(使用滑动手势的示例代码) 代码仅用于为您指明正确的方向。
我正在创建视图并将它们添加到 viewDidAppear 中的子视图,当然你可以在任何你想做的地方做(也可以在 viewDidLoad 中)。
添加两个属性
@property (nonatomic, strong) NSMutableArray *views;// to hold subviews
@property (nonatomic, assign) NSInteger index;// to keep track which view is currently visible
@property (nonatomic, assign) BOOL viewsCreated;
现在我要添加一些视图并将手势识别器添加到那里的超级视图
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// Adding an if condition so we create views only once other wise you can take this code to viewDidLoad method
if (!_viewCreated)
{
_viewsCreated = YES;
int xPos = 0;
_views = [NSMutableArray new];
for (int i = 0; i< 5; i ++)
{
CGRect frame = CGRectMake(xPos, 0, self.view.bounds.size.width, self.view.bounds.size.height);
if (i == 0)
{
// Create MapView object and add it to _views array
MKMapView *mapView = [[MKMapView alloc] initWithFrame:frame];
[self.view addSubview:mapView];
[_views addObject:mapView];
}
if (i == 1)
{
// Create tableView object and add it to _views array
UITableView *tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
//set tableView delegate and datasource
tableView.delegate = self;
tableView.dataSource = self;
[_views addObject:tableView];
[self.view addSubview:tableView];
}
if (i == 2)
{
// Create an imageView and add it to _views array
UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
//Set image
//imageView.image = <image>;
[_views addObject:imageView];
[self.view addSubview:imageView];
}
if (i == 3)
{
//Create whatever view you want and add it to array and subView as to self.view as well
}
if (i == 4)
{
//Create whatever view you want and add it to array and subView as to self.view as well
}
}
UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(onSwipeLeft:)];
leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:leftSwipe];
UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(onSwipeRight:)];
rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:rightSwipe];
}
}
- (void)onSwipeLeft:(UISwipeGestureRecognizer*)swipe
{
if (_index<4)
{
UIView *currentView = [_views objectAtIndex:_index];
CGRect frame = currentView.frame;
frame.origin.x = -self.view.bounds.size.width;
UIView *nextView = [_views objectAtIndex:_index+1];
[UIView animateWithDuration:0.5 animations:^{
currentView.frame = frame;
nextView.frame = self.view.bounds;
}];
_index++;
}
}
- (void)onSwipeRight:(UISwipeGestureRecognizer*)swipe
{
if (_index>1)
{
UIView *currentView = [_views objectAtIndex:_index];
CGRect frame = currentView.frame;
frame.origin.x = self.view.bounds.size.width;
UIView *nextView = [_views objectAtIndex:_index-1];
[UIView animateWithDuration:0.5 animations:^{
currentView.frame = frame;
nextView.frame = self.view.bounds;
}];
_index--;
}
}
我正在使用 *UISwipeGesture Recognizer,但现在只能使用两个视图,我们可以左右进行操作,但现在需要五个 UIview 才能显示为滑动,请帮帮我?
我只需要以下两个方向:
@左方向。 @正确的方向。
我想在我的视图中向左和向右滑动五个视图。
您可以通过iOS中的方法实现。
- 使用
UIScrollView
添加你想滑动的视图然后设置contentSize
,然后设置enablePaging
= YES,就完成了。 - 如果您的视图属于视图控制器,您可以使用
UIPageViewController
并添加 set viewControllers 属性 并设置滚动方向(垂直或水平)并完成。 如果你想通过
SwipeGestureRecognizer
处理它,你仍然可以这样做,在视图中添加手势识别器,然后将你想要滑动的所有视图作为子视图添加到其中,在向左和向右滑动时,您可以通过链接其框架将下一个或上一个视图放在前面,以跟踪哪个应该是当前可见视图以及上一个和下一个视图是什么,您可以保留一个适当的整数并在开始时将其值设置为 0 ,向左滑动递增 1,向右滑动递减,它应该始终为 0<=index<5.编辑(使用滑动手势的示例代码) 代码仅用于为您指明正确的方向。 我正在创建视图并将它们添加到 viewDidAppear 中的子视图,当然你可以在任何你想做的地方做(也可以在 viewDidLoad 中)。
添加两个属性
@property (nonatomic, strong) NSMutableArray *views;// to hold subviews
@property (nonatomic, assign) NSInteger index;// to keep track which view is currently visible
@property (nonatomic, assign) BOOL viewsCreated;
现在我要添加一些视图并将手势识别器添加到那里的超级视图
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// Adding an if condition so we create views only once other wise you can take this code to viewDidLoad method
if (!_viewCreated)
{
_viewsCreated = YES;
int xPos = 0;
_views = [NSMutableArray new];
for (int i = 0; i< 5; i ++)
{
CGRect frame = CGRectMake(xPos, 0, self.view.bounds.size.width, self.view.bounds.size.height);
if (i == 0)
{
// Create MapView object and add it to _views array
MKMapView *mapView = [[MKMapView alloc] initWithFrame:frame];
[self.view addSubview:mapView];
[_views addObject:mapView];
}
if (i == 1)
{
// Create tableView object and add it to _views array
UITableView *tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
//set tableView delegate and datasource
tableView.delegate = self;
tableView.dataSource = self;
[_views addObject:tableView];
[self.view addSubview:tableView];
}
if (i == 2)
{
// Create an imageView and add it to _views array
UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
//Set image
//imageView.image = <image>;
[_views addObject:imageView];
[self.view addSubview:imageView];
}
if (i == 3)
{
//Create whatever view you want and add it to array and subView as to self.view as well
}
if (i == 4)
{
//Create whatever view you want and add it to array and subView as to self.view as well
}
}
UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(onSwipeLeft:)];
leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:leftSwipe];
UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(onSwipeRight:)];
rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:rightSwipe];
}
}
- (void)onSwipeLeft:(UISwipeGestureRecognizer*)swipe
{
if (_index<4)
{
UIView *currentView = [_views objectAtIndex:_index];
CGRect frame = currentView.frame;
frame.origin.x = -self.view.bounds.size.width;
UIView *nextView = [_views objectAtIndex:_index+1];
[UIView animateWithDuration:0.5 animations:^{
currentView.frame = frame;
nextView.frame = self.view.bounds;
}];
_index++;
}
}
- (void)onSwipeRight:(UISwipeGestureRecognizer*)swipe
{
if (_index>1)
{
UIView *currentView = [_views objectAtIndex:_index];
CGRect frame = currentView.frame;
frame.origin.x = self.view.bounds.size.width;
UIView *nextView = [_views objectAtIndex:_index-1];
[UIView animateWithDuration:0.5 animations:^{
currentView.frame = frame;
nextView.frame = self.view.bounds;
}];
_index--;
}
}