1

我正在创建 UIButton 并将其动态添加到 UIView,例如:

UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];
btn.frame = CGRectMake(100, 10, btn.bounds.size.width, btn.bounds.size.height);
[btn addTarget:self action:@selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
[myView addSubview:btn];

这按预期工作, myAction 被调用。当我将其添加到可见 myView 的外部时,问题就开始了,例如:

btn.frame = CGRectMake(-100, 10, btn.bounds.size.width, btn.bounds.size.height);

稍后,我将 myView 动画出屏幕以使 btn 可见:

[UIView beginAnimations:nil context:NULL];
[myView layer].frame = CGRectMake(320, 0, 320, 48);
[UIView commitAnimations];

但问题是, myAction 不再被调用。为什么?以及如何解决?

4

3 回答 3

3

您对 Art 的回答的评论几乎解释了这个问题。您有一个按钮位于其父级的可见区域之外;如果父级的位置使得按钮在堆栈的更高位置可见,则无关紧要。按钮被剪裁而不是绘制。

你有两个选择。

1)使myView的框架更大并适当调整所有坐标。myView 将始终在屏幕外显示一些内容,在屏幕上显示一些内容,您可以简单地将其放置在您想要的位置。当按钮在屏幕上时,它将可见。(例如,在 myView 初始帧上方的代码中,将从 x 的某个负值开始并扩展到比 iPhone 屏幕宽得多,而您的 btn 将位于相对于 myView 左上角的某个正 x,y 位置. 然后你移动 myView,例如,它移动到 0,0,将它的一些内容推到屏幕外,同时将 btn 移动到屏幕上。)

2) 不要在 myView 上放置按钮;把它放在一个新的视图上,当你为 myView设置动画时,你可以其中设置动画。

#1 的简单代码:

// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(-100, 0, 420, 480)]; // start 100 pixels to the left
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];
    btn.frame = CGRectMake(0, 10, btn.bounds.size.width, btn.bounds.size.height);
    [myView addSubview:btn];

    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn1 setTitle:@"DoIt" forState:UIControlStateNormal];
    btn1.frame = CGRectMake(100, 100, 50, 50);
    [btn1 addTarget:self action:@selector(doit:) forControlEvents:UIControlEventTouchUpInside];
    [myView addSubview:btn1];
    self.view = myView;
}

- (void) doit: (id) bt {
    [UIView beginAnimations:nil context:NULL];
    self.view.frame = CGRectMake(100, 0, 420, 480);
    [UIView commitAnimations];
}
于 2009-11-06T04:01:50.790 回答
1

我在您的代码中注意到两件事:

首先,由于frame是相对于视图的父视图,所以主屏幕btn.frame不应该在屏幕外的位置,而是在myView. 假设myView已经在某个负 x 位置,按钮也会在那里。

其次,当你使用Core Animation时,你应该设置UIView你想要动画的属性,而不是UIView'图层的属性。

我对此进行了测试以确保这是我的代码:

[super viewDidLoad];
offscreenView = [[UIView alloc] initWithFrame:CGRectMake(-100, 0, 200, 200)];
offscreenView.backgroundColor = [UIColor blueColor];
UIButton * btn = [UIButton buttonWithType:UIButtonTypeContactAdd];
//note we put this at 10, 10 *relative to the button's superview*
btn.frame = CGRectMake(10.0, 10.0, 50.0, 25.0);
[btn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[offscreenView addSubview:btn];
[self.view addSubview:offscreenView];
[UIView beginAnimations:@"animateVIew" context:self];
offscreenView.frame = CGRectMake(0.0, 0.0, 200, 200);
[UIView setAnimationDuration:2.0];
[UIView commitAnimations];

希望有帮助!

于 2009-11-06T03:28:39.020 回答
1

CALayer 有一个 CATransform3D 属性

[[btn layer] setTransform:CATransform3DMakeTranslation(-100, 0, 0)];

and you can apply CAAnimation to this property

于 2014-04-16T19:21:11.157 回答