我使用* UISwipeGesture识别器,但现在的工作只有两个观点,我们可以做到这一点的左,右,但现在Ineed 5 的UIView显示为刷卡烦请帮我?iOS中使用uigesture recoginzier如何可以刷卡定制五个子视图目标C
我只需要两个方向如下:
@ leftft direction。 正确的方向。
我想滑动五个视图,即左视图和右视图。
我使用* UISwipeGesture识别器,但现在的工作只有两个观点,我们可以做到这一点的左,右,但现在Ineed 5 的UIView显示为刷卡烦请帮我?iOS中使用uigesture recoginzier如何可以刷卡定制五个子视图目标C
我只需要两个方向如下:
@ leftft direction。 正确的方向。
我想滑动五个视图,即左视图和右视图。
您可以通过以下方式在iOS中实现此目的。
UIScrollView
添加要刷卡的意见,然后设置contentSize
,并设置enablePaging
= YES,它的完成。UIPageViewController
并添加set viewControllers属性并设置滚动方向(垂直或水平)及其完成。如果你想通过SwipeGestureRecognizer
来处理它,你仍然可以做到这一点,在视图中添加手势识别器,然后添加所有想要扫过的视图作为其子视图,然后在左右滑动或前面的视图通过链接它的框架,跟踪哪些应该是当前可见视图,以及什么是前一个视图和下一个视图,您可以保留一个整数,并在开始时将其值设置为0,并在向左滑动时递增1向右滑动减量,应该始终为0 < =索引< 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--;
}
}
你已经尝试 – Naeem
@Naeem将增加手势识别:我试图到我的自定义视图来显示为滑块。当用户触摸向左方向时,它将显示第一个视图,而当用户触摸正确方向时,则会刷新第二个视图。所以我总共有五个子视图在我的self.view – MRJ
而不是使用滑动手势,使用旋转效果的卡集。 –