【发布时间】:2015-04-24 12:55:51
【问题描述】:
我的 ViewController 看起来非常庞大,现在有 500 多行代码。 我想对其进行重构,并将一些方法放入外部类或类别中。
但是我的很多方法都处理 IBOutlets 或 UIViewAnimation、动画 Storyboard 的 View 元素、隐藏它们、改变不透明度等。
将这些方法移出我的 ViewController 的最佳方法是什么?
- (void)animateBeforeNewRound;
- (void)animateBeforeNewGame;
- (void)animateBeforeFinishingTheGame;
- (void)presentSlidingView;
- (void)presentSlidingView {
//preparing view constraint for animation
__block float constr = 0;
[self.view.constraints enumerateObjectsUsingBlock:^(NSLayoutConstraint *constraint, NSUInteger idx, BOOL *stop) {
if (constraint.firstItem == self.slideView && constraint.firstAttribute == NSLayoutAttributeLeading) {
constr = constraint.constant;
[constraint setConstant:0.0f];
}
}];
[UIView animateWithDuration:0.1 animations:^{
/* animate prepared constraint */
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
/* animate constraints */
[UIView animateWithDuration:0.1 delay:0.2 options:UIViewAnimationOptionTransitionNone animations:^{
self.imgBubble.alpha = 1;
} completion:^(BOOL finished) {
/* update constraints back to initial value */
[self.view.constraints enumerateObjectsUsingBlock:^(NSLayoutConstraint *constraint, NSUInteger idx, BOOL *stop) {
if (constraint.firstItem == self.slide && constraint.firstAttribute == NSLayoutAttributeLeading) {
[constraint setConstant:constr];
}
}];
[UIView animateWithDuration:0.1 delay:0.8 options:UIViewAnimationOptionTransitionNone animations:^{
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
self.imgBubble.alpha = 0;
}];
}];
}];
}
- (void)animate* 方法看起来与- (void)presentSlidingView 非常相似,并使用其他 Storyboard 的 IBOutlets 进行操作。
【问题讨论】:
-
尝试为您的 viewController 创建一个自定义超类
-
@NJGadhiya 如果您的 VC 具有实现附加方法的 VC 的主要逻辑子类,这不是很奇怪吗?
标签: ios objective-c model-view-controller refactoring