簡體   English   中英

除了snapToPoint之外,還有其他東西在控制UISnapBehavior中的布局

[英]Something other than snapToPoint is controlling layout in UISnapBehavior

我是動畫新手,正在努力理解為什么UIAnimation和UISnapBehavior似乎會使UIButtons偏離中心。 顯然,除了我的代碼中還有什么控制布局。 也許有人可以告訴我我所缺少的。

最初,我的ViewController在程序設計上(如圖X所示)在圓的圓周上放置了五個UIButton(即沒有XIB文件)。在修改我的應用程序時,我引入了捕捉動畫以將UIButtons從中心點移到適當位置。 圖B顯示了動畫開始的中心點。 圖C顯示了五個UIButton,即使在snapToPoint:上使用的snapToPoint:與在放置UIButton時使用的snapToPoint:完全相同,在動畫結束后仍將其移位。

在此處輸入圖片說明

當我單擊屏幕底部的黑色矩形區域時,我注意到unexpected nil window in _UIApplicationHandleEventFromQueueEvent記錄的報告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位於圓的圓周上。 它們替換了上面定義originCGRect左上角頂點的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];
}

然后在動畫開始之前將五個UIButton放置在中心,我通過添加將每個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;

然后我通過在viewDidLoad之后立即插入指針將UISnapBehaviour添加到動畫中

UIDynamicAnimator* _animator1;
UIDynamicAnimator* _animator2;
UIDynamicAnimator* _animator3;
UIDynamicAnimator* _animator4;
UIDynamicAnimator* _animator5;
UISnapBehavior* _snap1;
UISnapBehavior* _snap2;
UISnapBehavior* _snap3;
UISnapBehavior* _snap4;
UISnapBehavior* _snap5;

並在[super viewDidLoad];之前添加UIDynamicAnimatorUISnapBehavior方法[super viewDidLoad]; 導致如圖C所示的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");
}

問題已經解決了。 在較早的草圖中, origin坐標是左上頂點的坐標。 必須重新定義origin以使CGRect在圓的圓周上。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM