我觉得难以写的是视图控制器之间的交互式转换.举个例子,当我只想用推送动画替换另一个视图时,我可以使用UINavigationController或使用包含API并自己编写转换.但是我希望转换是交互式的,触摸控制的很常见:用户开始拖动当前视图,从侧面显示传入视图,并且通过平移触摸手势控制转换.用户可以稍微平移以“窥视”传入视图,然后向后平移,保持当前视图可见.如果手势结束低于某个阈值,则取消转换,否则完成.
(如果这个不够清楚,我说的是像iBooks中的翻页,但是在不同的视图控制器之间,并且推广到任何这样的交互式转换.)
我知道如何编写这样的转换,但是当前的视图控制器必须对转换了解太多 – 它占用了过多的代码.并且甚至没有提到可以容易地进行两种不同的交互式转换,在这种情况下,所讨论的控制器充满了与其紧密耦合的转换代码.
是否有抽象的模式,概括交互式转换代码并将其移动到单独的类或代码块中?也许是图书馆?
解决方法 我不知道有任何库可以做到这一点,但是我已经用UIVIEwController上的类别抽象出了转换代码,或者为我的视图控制器创建了一个基类,其中包含了转换代码.我将所有混乱的转换代码保存在基类中,在我的控制器中,我只需要添加一个手势识别器并从其action方法中调用基类方法.-(IBAction)dragInController:(UIPanGestureRecognizer *)sender { [self dragController:[self.storyboard instantiateVIEwControllerWithIDentifIEr:@"GenericVC"] sender:sender];}
编辑后:
这是我的一次尝试.这是DragIntoBaseVC中的代码,它是另一个控制器需要继承的控制器,使用上面的代码将视图拖入其中.这只处理拖拽(仅从右侧),而不是拖拽(仍在处理拖拽,以及如何使这个方向相对于方向更通用).很多代码都在那里处理轮换.它适用于任何方向(颠倒除外),适用于iPhone和iPad.我通过动画布局约束而不是设置框架来制作动画,因为这似乎是苹果公司的方向(我怀疑他们将来会贬低旧的支柱和d簧系统).
#import "DragIntoBaseVC.h"@interface DragIntoBaseVC ()@property (strong,nonatomic) NSLayoutConstraint *leftCon;@property (strong,nonatomic) UIVIEwController *incomingVC;@property (nonatomic) NSInteger w;@end@implementation DragIntoBaseVCstatic int first = 1;-(voID)dragController:(UIVIEwController *) incomingVC sender:(UIPanGestureRecognizer *) sender { if (first) { self.incomingVC = incomingVC; UIVIEw *inVIEw = incomingVC.vIEw; [inVIEw setTranslatesautoresizingMaskIntoConstraints:NO]; inVIEw.transform = self.vIEw.transform; [self.vIEw.window addSubvIEw:inVIEw]; self.w = self.vIEw.bounds.size.wIDth; NSLayoutConstraint *con2; switch ([UIDevice currentDevice].orIEntation) { case 0: case 1: self.leftCon = [NSLayoutConstraint constraintWithItem:inVIEw attribute:NSLayoutAttributeleft relatedBy:0 toItem:self.vIEw attribute:NSLayoutAttributeleft multiplIEr:1 constant:self.w]; con2 = [NSLayoutConstraint constraintWithItem:self.vIEw attribute:NSLayoutAttributetop relatedBy:0 toItem:inVIEw attribute:NSLayoutAttributetop multiplIEr:1 constant:0]; break; case 3: self.leftCon = [NSLayoutConstraint constraintWithItem:inVIEw attribute:NSLayoutAttributeBottom relatedBy:0 toItem:self.vIEw attribute:NSLayoutAttributeBottom multiplIEr:1 constant:self.w]; con2 = [NSLayoutConstraint constraintWithItem:self.vIEw attribute:NSLayoutAttributeleft relatedBy:0 toItem:inVIEw attribute:NSLayoutAttributeleft multiplIEr:1 constant:0]; break; case 4: self.leftCon = [NSLayoutConstraint constraintWithItem:inVIEw attribute:NSLayoutAttributetop relatedBy:0 toItem:self.vIEw attribute:NSLayoutAttributetop multiplIEr:1 constant:-self.w]; con2 = [NSLayoutConstraint constraintWithItem:self.vIEw attribute:NSLayoutAttributeRight relatedBy:0 toItem:inVIEw attribute:NSLayoutAttributeRight multiplIEr:1 constant:0]; break; default: break; } NSLayoutConstraint *con3 = [NSLayoutConstraint constraintWithItem:self.vIEw attribute:NSLayoutAttributeWIDth relatedBy:0 toItem:inVIEw attribute:NSLayoutAttributeWIDth multiplIEr:1 constant:0]; NSLayoutConstraint *con4 = [NSLayoutConstraint constraintWithItem:self.vIEw attribute:NSLayoutAttributeHeight relatedBy:0 toItem:inVIEw attribute:NSLayoutAttributeHeight multiplIEr:1 constant:0]; NSArray *constraints = @[self.leftCon,con2,con3,con4]; [self.vIEw.window addConstraints:constraints]; first = 0; } CGPoint translate = [sender translationInVIEw:self.vIEw]; if ([UIDevice currentDevice].orIEntation == 0 || [UIDevice currentDevice].orIEntation == 1 || [UIDevice currentDevice].orIEntation == 3) { // for portrait or landscapeRight if (sender.state == UIGestureRecognizerStateBegan || sender.state == UIGestureRecognizerStateChanged) { self.leftCon.constant += translate.x; [sender setTranslation:CGPointZero inVIEw:self.vIEw]; }else if (sender.state == UIGestureRecognizerStateEnded){ if (self.leftCon.constant < self.w/2) { [self.vIEw removeGestureRecognizer:sender]; [self finishTransition]; }else{ [self abortTransition:1]; } } }else{ // for landscapeleft if (sender.state == UIGestureRecognizerStateBegan || sender.state == UIGestureRecognizerStateChanged) { self.leftCon.constant -= translate.x; [sender setTranslation:CGPointZero inVIEw:self.vIEw]; }else if (sender.state == UIGestureRecognizerStateEnded){ if (-self.leftCon.constant < self.w/2) { [self.vIEw removeGestureRecognizer:sender]; [self finishTransition]; }else{ [self abortTransition:-1]; } } }}-(voID)finishTransition { self.leftCon.constant = 0; [UIVIEw animateWithDuration:.3 animations:^{ [self.vIEw.window layoutSubvIEws]; } completion:^(BOol finished) { self.vIEw.window.rootVIEwController = self.incomingVC; }];}-(voID)abortTransition:(int) sign { self.leftCon.constant = self.w * sign; [UIVIEw animateWithDuration:.3 animations:^{ [self.vIEw.window layoutSubvIEws]; } completion:^(BOol finished) { [self.incomingVC.vIEw removeFromSupervIEw]; // this line and the next reset the system back to the inital state. first = 1; }];}总结
以上是内存溢出为你收集整理的ios – 视图控制器之间的交互式转换?全部内容,希望文章能够帮你解决ios – 视图控制器之间的交互式转换?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)