gpt4 book ai didi

ios - 三 View 之间使用UIScrollView到 "rotate"

转载 作者:搜寻专家 更新时间:2023-10-30 20:17:36 26 4
gpt4 key购买 nike

我有一个 UIScrollView我添加了三个 View Z , AB因此:

enter image description here

我想要完成的是以 View 为中心的“旋转”方式设置三个 View A首先出现。当用户向左滑动并看到 View 时 B , 再次向滑动可以查看Z ,反之亦然,在查看时向滑动Z带用户查看B .

我已经为 self.scrollview 设置了代码因此:

ZViewController *zViewController = [[ZViewController alloc] init];
[self addChildViewController:zViewController];
[self.scrollView addSubview:zViewController.view];
[zViewController didMoveToParentViewController:self];

AViewController *aViewController = [[AViewController alloc] init];
CGRect aframe = aViewController.view.frame;
aframe.origin.x = 320;
aViewController.view.frame = aframe;
[self addChildViewController:aViewController];
[self.scrollView addSubview:aViewController.view];
[aViewController didMoveToParentViewController:self];

BViewController *bViewController = [[BViewController alloc] init];
CGRect bframe = bViewController.view.frame;
bframe.origin.x = 640;
bViewController.view.frame = bframe;
[self addChildViewController:bViewController];
[self.scrollView addSubview:bViewController.view];
[bViewController didMoveToParentViewController:self];

self.scrollView.contentSize = CGSizeMake(960, self.view.frame.size.height);
self.scrollView.pagingEnabled = YES;

但是,目前我不确定如何继续让“旋转”元素工作(即在 B 上滑动到 Z )并希望得到任何帮助。

谢谢!

最佳答案

如果您想使用 3 个 View Controller 而不是具有 3 个页面的 ScrollView ,这个使用标签栏 Controller 的简单实现应该适合您。我从选项卡式应用程序模板开始,并添加了第三个 View Controller 。索引 0、1 和 2 处的 Controller 分别对应于 View A、B 和 Z。在A Controller 的viewDidLoad方法中,我设置了标签栏隐藏。

我创建了一个 BaseViewController 类,我在 Storyboard中设置的 3 个 Controller 继承自该类。 BaseViewController 中的代码创建并添加滑动手势识别器,以为您提供旋转顺序的方式处理滑动,并为您提供 View 之间的滑动过渡,

@implementation BaseViewController


- (void)viewDidLoad {
[super viewDidLoad];
UISwipeGestureRecognizer *swiperRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
swiperRight.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swiperRight];

UISwipeGestureRecognizer *swiperLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
swiperLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swiperLeft];
}

- (void)handleSwipe:(UISwipeGestureRecognizer *)sender {
if (sender.direction == 1) {
NSInteger nextIndex = (self.tabBarController.selectedIndex - 1 == -1)? 2 : self.tabBarController.selectedIndex - 1;
[self slideInViewWithIndex:nextIndex direction:-1];
}else{
NSInteger nextIndex = (self.tabBarController.selectedIndex + 1 == 3)? 0 : self.tabBarController.selectedIndex + 1;
[self slideInViewWithIndex:nextIndex direction:1];
}
}


-(void)slideInViewWithIndex:(NSInteger) index direction:(NSInteger) dir {
UIView *nextView = [(self.tabBarController.viewControllers[index]) view];
[self.tabBarController.view addSubview:nextView];
nextView.frame = CGRectOffset(self.tabBarController.view.bounds, dir * self.tabBarController.view.bounds.size.width, 0);
[UIView animateWithDuration:.3 animations:^{
nextView.frame = self.tabBarController.view.bounds;
} completion:^(BOOL finished) {
self.tabBarController.selectedIndex = index;
}];
}

关于ios - 三 View 之间使用UIScrollView到 "rotate",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23901729/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com