我是动画新手,很难理解为什么 UIAnimation 和 UISnapBehavior 似乎会使 UIButtons 偏离中心。显然,除了我的代码之外,还有一些控制布局的东西。也许有人可以告诉我我错过了什么。
最初我的 ViewController 有五个 UIButtons 以编程方式(即没有 XIB 文件)放置在圆的圆周上,如图 A 所示。在修改我的应用程序时,我引入了快照动画以将 UIButtons 从中心点移动到位置。图 B 显示了动画开始的中心点。图 C 显示了在动画结束后被替换的五个 UIButtons,即使snapToPoint:
用于放置 UIButtons 的 CGPoints 与用于放置 UIButtons 的 CGPoints 完全相同。
当我单击屏幕底部的黑色矩形区域时,我注意到记录的报告中有一条消息,unexpected nil window in _UIApplicationHandleEventFromQueueEvent
其中包括框架的大小属性。这表明帧或边界可能是 UIButton 在动画后偏离中心的原因。
这是我用来演示发生了什么的代码的最新版本。我相信它是正确的,即使它不会在面向对象的选美比赛中赢得任何奖项 :-)
首先,如图A所示的五个没有动画的按钮。
- (void)viewDidLoad {
NSLog(@"load GlobalView");
CGRect dot1 = (CGRect) { .origin.x = 38.0f, .origin.y = 263.0f, .size.width = 80.0f, .size.height = 80.0f,};
CGRect dot2 = (CGRect) { .origin.x = 207.0f, .origin.y = 263.0f, .size.width = 80.0f, .size.height = 80.0f,};
CGRect dot3 = (CGRect) { .origin.x = 123.0f, .origin.y = 320.0f, .size.width = 80.0f, .size.height = 80.0f,};
CGRect dot4 = (CGRect) { .origin.x = 170.0f, .origin.y = 167.0f, .size.width = 80.0f, .size.height = 80.0f,};
CGRect dot5 = (CGRect) { .origin.x = 70.0f, .origin.y = 167.0f, .size.width = 80.0f, .size.height = 80.0f,};
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~
编辑。以下代码行重新定义了origin
以CGRect
圆的圆周为中心。它们替换了上面定义origin
为 的左上顶点的行 CGRect
。动画现在根据图 A 所示的布局放置 UIButton。
*/
float width = 80.0f;
float height = 80.0f;
CGRect dot1 = (CGRect) { .origin.x = 38.0f + width/2, .origin.y = 263.0f + width/2, width, height};
CGRect dot2 = (CGRect) { .origin.x = 207.0f + width/2, .origin.y = 263.0f + width/2, width, height};
CGRect dot3 = (CGRect) { .origin.x = 123.0f + width/2, .origin.y = 320.0f + width/2, width, height};
CGRect dot4 = (CGRect) { .origin.x = 170.0f + width/2, .origin.y = 167.0f + width/2, width, height};
CGRect dot5 = (CGRect) { .origin.x = 70.0f + width/2, .origin.y = 167.0f + width/2, width, height};
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~
// Insert global rings
UIView *ringView = [[UIView alloc] initWithFrame:CGRectMake(60.0f, 178.0f, 200.0f, 200.0f)];
UIImage *ringImage = [UIImage imageNamed:@"GlobalRings.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:ringImage];
imageView.frame = ringView.bounds; // frame imageView in ringView and fill ringView
[ringView addSubview:imageView]; // add the imageview to the superview
[self.view addSubview:ringView]; //add the view to the main view
// place 5 UIButtons
UIButton *navigationButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
[navigationButton1 setTitleColor:[UIColor blackColor] forState: UIControlStateNormal];
navigationButton1.frame = CGRectMake(38.0, 263.0, 80.0, 80.0);
[navigationButton1 setImage:[UIImage imageNamed:@"Dot1.png"] forState:UIControlStateNormal];
[navigationButton1 addTarget:self action:@selector(goToVenue1) forControlEvents:UIControlEventTouchUpInside];
UIButton *navigationButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
[navigationButton2 setTitleColor:[UIColor blackColor] forState: UIControlStateNormal];
navigationButton2.frame = CGRectMake(207.0, 263.0, 80.0, 80.0);
[navigationButton2 setImage:[UIImage imageNamed:@"Dot2.png"] forState:UIControlStateNormal];
[navigationButton2 addTarget:self action:@selector(goToVenue2) forControlEvents:UIControlEventTouchUpInside];
UIButton *navigationButton3 = [UIButton buttonWithType:UIButtonTypeCustom];
[navigationButton3 setTitleColor:[UIColor blackColor] forState: UIControlStateNormal];
navigationButton3.frame = CGRectMake(123.0, 320.0, 80.0, 80.0);
[navigationButton3 setImage:[UIImage imageNamed:@"Dot3.png"] forState:UIControlStateNormal];
[navigationButton3 addTarget:self action:@selector(goToVenue3) forControlEvents:UIControlEventTouchUpInside];
UIButton *navigationButton4 = [UIButton buttonWithType:UIButtonTypeCustom];
[navigationButton4 setTitleColor:[UIColor blackColor] forState: UIControlStateNormal];
navigationButton4.frame = CGRectMake(170.0, 167.0, 80.0, 80.0);
[navigationButton4 setImage:[UIImage imageNamed:@"Dot4.png"] forState:UIControlStateNormal];
[navigationButton4 addTarget:self action:@selector(goToVenue4) forControlEvents:UIControlEventTouchUpInside];
UIButton *navigationButton5 = [UIButton buttonWithType:UIButtonTypeCustom];
[navigationButton5 setTitleColor:[UIColor blackColor] forState: UIControlStateNormal];
navigationButton5.frame = CGRectMake(70.0, 167.0, 80.0, 80.0);
[navigationButton5 setImage:[UIImage imageNamed:@"Dot5.png"] forState:UIControlStateNormal];
[navigationButton5 addTarget:self action:@selector(goToVenue5) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:navigationButton1];
[self.view addSubview:navigationButton2];
[self.view addSubview:navigationButton3];
[self.view addSubview:navigationButton4];
[self.view addSubview:navigationButton5];
[super viewDidLoad];
}
然后在动画开始之前将五个 UIButtons 放置在中心,我通过添加将每个 UIButton 与框架中心对齐
CGRect preSnap = (CGRect) { .origin.x = 120.0f, .origin.y = 240.0f, .size.width = 80.0f, .size.height = 80.0f,};
然后更改每个按钮帧(如下)以设置动画的起点
navigationButton1.frame = preSnap;
navigationButton2.frame = preSnap;
navigationButton3.frame = preSnap;
navigationButton4.frame = preSnap;
navigationButton5.frame = preSnap;
然后我UISnapBehaviour
通过在之后立即插入指针来添加到动画中viewDidLoad
UIDynamicAnimator* _animator1;
UIDynamicAnimator* _animator2;
UIDynamicAnimator* _animator3;
UIDynamicAnimator* _animator4;
UIDynamicAnimator* _animator5;
UISnapBehavior* _snap1;
UISnapBehavior* _snap2;
UISnapBehavior* _snap3;
UISnapBehavior* _snap4;
UISnapBehavior* _snap5;
并在生成图 C 所示布局 之前添加UIDynamicAnimator
和方法UISnapBehavior
[super viewDidLoad];
UIButton
_animator1 = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
_snap1 = [[UISnapBehavior alloc] initWithItem:navigationButton1 snapToPoint: dot1.origin];
[_animator1 addBehavior:_snap1];
_animator2 = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
_snap2 = [[UISnapBehavior alloc] initWithItem:navigationButton2 snapToPoint: dot2.origin];
[_animator2 addBehavior:_snap2];
_animator3 = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
_snap3 = [[UISnapBehavior alloc] initWithItem:navigationButton3 snapToPoint: dot3.origin];
[_animator3 addBehavior:_snap3];
_animator4 = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
_snap4 = [[UISnapBehavior alloc] initWithItem:navigationButton4 snapToPoint: dot4.origin];
[_animator4 addBehavior:_snap4];
_animator5 = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
_snap5 = [[UISnapBehavior alloc] initWithItem:navigationButton5 snapToPoint: dot5.origin];
[_animator5 addBehavior:_snap5];
[super viewDidLoad];
}
最后我添加了五种方法来确保所有按钮都能正常工作
- (void)goToVenue1 {
NSLog(@"Button 1 worked");
}
- (void)goToVenue2 {
NSLog(@"Button 2 worked");
}
- (void)goToVenue3 {
NSLog(@"Button 3 worked");
}
- (void)goToVenue4 {
NSLog(@"Button 4 worked");
}
- (void)goToVenue5 {
NSLog(@"Button 5 worked");
}