2

我如何摆脱这些警告

“警告:'UIButton' 可能不会响应'-setPosition:'

“警告:'UIButton' 可能不会响应 '-addAnimation:forKey'

我在这里得到它们:

- (void)monBtnIn {

[monButton setPosition:CGPointMake(113.5,256.5)];
[monButton addAnimation:[self monInAnimation] 
                    forKey:@"position"];

}

一切构建和加载都很好,但我不喜欢警告。我认为这与它是 UIButton 而不是 CALayer 的事实有关。

那么,如何使 UIButton 工作?

.h 文件

IBOutlet UIButton *monButton;

这是 KeyFrameAnimation - 它创建了一个“有弹性的”动画来解决。

- (CAAnimation*)monInAnimation {
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path,NULL,113,520);
CGPathAddLineToPoint(path, NULL, 113.5, 286);
CGPathAddLineToPoint(path, NULL, 113.5, 242);
CGPathAddLineToPoint(path, NULL, 113.5, 270);  
CGPathAddLineToPoint(path, NULL, 113.5, 250);
CGPathAddLineToPoint(path, NULL, 113.5, 262);
CGPathAddLineToPoint(path, NULL, 113.5, 254);
CGPathAddLineToPoint(path, NULL, 113.5, 258);
CGPathAddLineToPoint(path, NULL, 113.5, 256.5);

CAKeyframeAnimation *
animation = [CAKeyframeAnimation 
             animationWithKeyPath:@"position"];

[animation setPath:path];
[animation setDuration:1.5];
NSArray *arr = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0], 
 [NSNumber numberWithFloat:0.12], 
 [NSNumber numberWithFloat:0.24], 
 [NSNumber numberWithFloat:0.36], 
 [NSNumber numberWithFloat:0.48],
 [NSNumber numberWithFloat:0.60],
 [NSNumber numberWithFloat:0.72],
 [NSNumber numberWithFloat:0.84],
 [NSNumber numberWithFloat:1.0],nil];

 [animation setKeyTimes:arr];
 [animation setTimingFunctions:[NSArray arrayWithObjects:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn],
                                [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut], nil]];
 //[animation setAutoreverses:YES];
CFRelease(path);
return animation;

}

非常感谢您的帮助!

4

2 回答 2

3

您是否尝试过使用

[monButton.layer setPosition:CGPointMake(113.5,256.5)];
[monButton.layer addAnimation:[self monInAnimation] 
                    forKey:@"position"];
于 2010-01-03T10:55:52.653 回答
2

我强烈建议你在你的 iPhone 项目中打开“Objective-C warnings as errors”编译器选项;我在我的博客上写了一篇文章,解释了如何做到这一点:

http://akosma.com/2009/07/16/objective-c-compiler-warnings/

这将帮助您避免像这样的运行时问题。恕我直言,您永远不应该发布带​​有警告的代码。

于 2010-01-03T14:21:12.697 回答