ben*_*der 7 iphone cocoa-touch uibutton uiview
所以我的按钮和动画有问题.基本上,我正在使用UIView动画设置动画,同时还试图在视图内的按钮上轻击.视图与按钮一样大,视图实际上是UIImageView的子类,按钮下方有一个图像.该视图是在Interface Builder中放置的容器视图的子视图,启用了用户交互并启用了剪切.所有动画和按钮处理都在此UIImageView子类中完成,而startFloating消息根据需要从单独的类发送.
如果我没有动画,则buttonTapped:消息会正确发送,但在动画期间它不会被发送.我也尝试过实现该touchesEnded方法,并发生相同的行为.
UIImageView子类init(我有一个颜色的按钮,所以我可以看到框架设置正确,它做了):
- (id)initWithImage:(UIImage *)image {
self = [super initWithImage:image];
if (self != nil) {
// ...stuffs
UIButton *tapBtn = [UIButton buttonWithType:UIButtonTypeCustom];
tapBtn.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
[tapBtn addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
tapBtn.backgroundColor = [UIColor cyanColor];
[self addSubview:tapBtn];
self.userInteractionEnabled = YES;
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
启动动画的动画方法(如果我不调用此按钮可以正常工作):
- (void)startFloating {
[UIView beginAnimations:@"floating" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDuration:10.0f];
self.frame = CGRectMake(self.frame.origin.x, -self.frame.size.height, self.frame.size.width, self.frame.size.height);
[UIView commitAnimations];
}
Run Code Online (Sandbox Code Playgroud)
所以,要明确:
小智 20
这解决了这个问题:
[UIView animateWithDuration:20 delay: 0.0 options: UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction
...
Run Code Online (Sandbox Code Playgroud)
...遇到了同样的问题,因为我的代码每个块都在做一个大型动画.我制作了一个基于NSTimer的解决方案,就像上面建议的一样,但是它起作用了......但是运动是生涩的(除非我在每个定时器事件触发器中插入动画).
因此,既然需要动画,我找到了一个不需要计时器的解决方案.它只是短距离动画,因此按钮点击仍然准确,只有一个小错误,我的情况在UI中非常不明显,并且可以根据你的参数减少.
请注意,任何给定时间的误差都小于15.0,根据您的动画速度要求,可以降低误差以获得更高的精度.您还可以缩短持续时间以获得更快的速度.
- (void)conveyComplete:(UIView*)v
{
[self convey:v delay:0];
}
- (void)convey:(UIView*)v delay:(int)nDelay
{
[UIView animateWithDuration:.5
delay:nDelay
options:(UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction)
animations: ^
{
CGRect rPos = v.frame;
rPos.origin.x -= 15.0;
v.frame = rPos;
}
completion: ^(BOOL finished)
{
[self conveyComplete:v];
}];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4168 次 |
| 最近记录: |