gpt4 book ai didi

iphone - 当前的 ChildView Controller

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:31:09 24 4
gpt4 key购买 nike

我正在尝试在我的 View Controller 上实现 SwipeGesture。

我目前面临的问题是我无法确定当前显示的 subview Controller 是什么。

swipeGesture 被添加到容器 View Controller 。然后必须判断当前显示的VC在父子关系中是什么,然后向右或向左移动。

最佳答案

如果您在谈论自定义容器 View Controller ,容器 Controller 的工作就是跟踪它。因此,您可能拥有自己的 @property 来跟踪您所处的位置,并在 transitionFromController 滑动时调整它。因此,您可能有一个数字属性,当您向右移动时递增,向左移动时递减。

一般来说(如果您只是想跟踪您传递给 transitionFromViewController 的是哪个“来自” Controller ),有两种方法。 Adding a child controller 中的 list 14-3 暗示了一种方法View Controller 编程指南。

在这种情况下,在 viewDidLoad 中,您为第一个 View Controller 执行 addChildViewController。不过,在这种情况下,此时您不会通过 addChildViewController 加载其他 subview Controller ,而是让执行转换的方法来处理它(如 list 14-3 所示) ).

如果您这样做,您只需获取 [self.childViewControllers lastObject],这将是您的“当前”子 Controller 。所以,这可能看起来像:

@interface ViewController ()

@property (nonatomic) NSInteger currentIndex;

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];

UIViewController *initialController = ... // set your initial controller

[self addChildViewController:initialController];
initialController.view.frame = self.containerView.bounds;
[self.containerView addSubview:initialController.view];
[initialController didMoveToParentViewController:self];

UISwipeGestureRecognizer *gesture;

gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
gesture.direction = UISwipeGestureRecognizerDirectionRight;
[self.containerView addGestureRecognizer:gesture];

gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
gesture.direction = UISwipeGestureRecognizerDirectionLeft;
[self.containerView addGestureRecognizer:gesture];

}

- (void) handleSwipe:(UISwipeGestureRecognizer *)gesture
{
if (gesture.direction == UISwipeGestureRecognizerDirectionLeft && self.currentIndex < (kMaxIndex - 1))
{
self.currentIndex++;

UIViewController *newController = ... // set your new controller
UIViewController *oldController = [self.childViewControllers lastObject];

[self cycleFromViewController:oldController
toViewController:newController
direction:gesture.direction];

}
else if (gesture.direction == UISwipeGestureRecognizerDirectionRight && self.currentIndex > 0)
{
self.currentIndex--;

UIViewController *newController = ... // set your new controller
UIViewController *oldController = [self.childViewControllers lastObject];

[self cycleFromViewController:oldController
toViewController:newController
direction:gesture.direction];
}
}

- (void) cycleFromViewController:(UIViewController*) oldController
toViewController:(UIViewController*) newController
direction:(UISwipeGestureRecognizerDirection)direction
{
[oldController willMoveToParentViewController:nil];
[self addChildViewController:newController];

newController.view.frame = oldController.view.frame;

UIViewAnimationOptions options;

if (direction == UISwipeGestureRecognizerDirectionRight)
options = UIViewAnimationOptionTransitionFlipFromLeft;
else if (direction == UISwipeGestureRecognizerDirectionLeft)
options = UIViewAnimationOptionTransitionFlipFromRight;

[self transitionFromViewController:oldController
toViewController:newController
duration:0.33
options:options
animations:^{
[oldController removeFromParentViewController];
}
completion:^(BOOL finished) {
[newController didMoveToParentViewController:self];
}];
}

我看到有人做的另一种模型是通过 viewDidLoad 中的 addChildViewController 加载所有潜在的子 Controller 。我个人不喜欢这种方法,但我知道人们这样做并且效果很好。

但是如果你这样做,你就不能再依赖 childViewControllers 来知道哪个 Controller 是当前 Controller 。在这种情况下,您必须为 currentChildController 定义自己的类属性。所以它可能看起来像:

@interface ViewController ()

@property (nonatomic, strong) UIViewController *currentChildController;
@property (nonatomic) NSInteger currentIndex;

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];

[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"ChildOne"]];
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"ChildTwo"]];
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"ChildThree"]];

self.currentChildController = self.childViewControllers[0];

self.currentChildController.view.frame = self.containerView.bounds;
[self.containerView addSubview:self.currentChildController.view];

for (UIViewController *controller in self.childViewControllers)
[controller didMoveToParentViewController:self];

UISwipeGestureRecognizer *gesture;

gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
gesture.direction = UISwipeGestureRecognizerDirectionRight;
[self.containerView addGestureRecognizer:gesture];

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

- (void) cycleFromViewController:(UIViewController*) oldController
toViewController:(UIViewController*) newController
direction:(UISwipeGestureRecognizerDirection) direction
{
self.currentChildController = newController;

newController.view.frame = oldController.view.frame;

UIViewAnimationOptions options;

if (direction == UISwipeGestureRecognizerDirectionRight)
options = UIViewAnimationOptionTransitionFlipFromLeft;
else if (direction == UISwipeGestureRecognizerDirectionLeft)
options = UIViewAnimationOptionTransitionFlipFromRight;

[self transitionFromViewController:oldController
toViewController:newController
duration:0.33
options:options
animations:^{
}
completion:nil];
}

- (void) handleSwipe:(UISwipeGestureRecognizer *)gesture
{
if (gesture.direction == UISwipeGestureRecognizerDirectionLeft && self.currentIndex < (kMaxIndex - 1))
{
self.currentIndex++;

UIViewController *oldController = self.currentChildController;
UIViewController *newController = self.childViewControllers[self.currentIndex];

[self cycleFromViewController:oldController
toViewController:newController
direction:gesture.direction];

}
else if (gesture.direction == UISwipeGestureRecognizerDirectionRight && self.currentIndex > 0)
{
self.currentIndex--;

UIViewController *oldController = self.currentChildController;
UIViewController *newController = self.childViewControllers[self.currentIndex];

[self cycleFromViewController:oldController
toViewController:newController
direction:gesture.direction];
}
}

这是两种合乎逻辑的方法。

顺便说一下,第一种方法并不排除让您自己的类属性知道您在哪个 Controller 上,有时这很有用(例如,如果您使用的动画类型取决于您是否要“右”或“左”),但除非是这种情况,否则您只需查看 [self.childViewControllers lastObject] 即可。

关于iphone - 当前的 ChildView Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14405490/

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