我正在尝试创建一个"帮助类"动画UI元素,但我没有成功。我应该说我还是iOS编程的初学者,我喜欢以编程方式做任何事情。
我正在做的是:
Animator *anim;
myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[myButton addTarget:self action:@selector(addToFav) forControlEvents:UIControlEventTouchUpInside];
[myButton setTitle:@"+" forState:UIControlStateNormal];
[myButton setFont:[UIFont systemFontOfSize:23]];
[myButton setBackgroundColor:[UIColor greenColor]];
[myButton setFrame:CGRectMake(screenWidth * 0.9 - (SizeX / 2), screenHeight * 0.2285 - (SizeY / 2), X, Y)];
[self.view addSubview: myButton];
[anim moveButton:&myButton alongX:-100 alongY:200 withTime:1.5];
这是
Animator.h
@interface Animator : NSObject
-(void)moveButton:(UIButton*)button alongX:(int)x alongY:(int)y withTime:(float)time;
-(void)scaleButton:(UIButton*)button width:(float)width height:(float)height withTime:(float)time;
@end
Animator.m
@implementation Animator
-(void)moveButton:(UIButton*)button alongX:(int)x alongY:(int)y withTime:(float)time
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:time];
[UIView setAnimationBeginsFromCurrentState:YES];
[button setFrame:CGRectMake(button.frame.origin.x + x, button.frame.origin.y + y, button.frame.size.width, button.frame.size.height)];
[UIView commitAnimations];
}
-(void)scaleButton:(UIButton*)button width:(float)width height:(float)height withTime:(float)time
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:time];
[UIView setAnimationBeginsFromCurrentState:YES];
[button setFrame:CGRectMake(button.frame.origin.x, button.frame.origin.y, button.frame.size.width * width, button.frame.size.height * height)];
[UIView commitAnimations];
}
@end
编译器告诉它不能传递UIButton的地址,因为ARC,我不想禁用它,我甚至不知道它会有多少帮助禁用ARC。
有没有办法通过引用传递UIButton?
测试代码:
#import "ViewController.h"
#import "Animator.h"
@interface ViewController ()
{
UIButton* myButton;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib
myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[myButton addTarget:self action:@selector(move) forControlEvents:UIControlEventTouchUpInside];
[myButton setTitle:@"+" forState:UIControlStateNormal];
[myButton setFont:[UIFont systemFontOfSize:23]];
[myButton setBackgroundColor:[UIColor greenColor]];
[myButton setFrame:CGRectMake(0, 0, 100, 100)];
[self.view addSubview: myButton];
}
-(void)move
{
Animator *anim;
[anim moveButton:myButton alongX:-100 alongY:200 withTime:1.5];
/*[UIView animateWithDuration:1.5 animations:^{ // animate
myButton.frame = CGRectOffset(myButton.frame, 0, 200); // set new frame but with specified offset
}];*/
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
答案 0 :(得分:4)
请改用:
[anim moveButton:myButton alongX:-100 alongY:200 withTime:1.5]; // no asterisk!
请注意我相信你已经初始化的myButton:
UIButton *myButton = [[UIButton alloc] init];
A POINTER 不是对象本身。所以使用& myButton意味着:获取指针的地址!但是,如您所见,您不需要指针的地址而是按钮的地址。只需传递myButton,因为它已经是指向myButton的指针。
另一种方法
这里我有一个简单的代码片段,应该适合你。如果没有,问题出在其他地方。
-(void)moveButton:(UIButton*)button alongX:(int)x alongY:(int)y withTime:(float)time
{
[UIView animateWithDuration:time animations:^{ // animate
button.frame = CGRectOffset(button.frame, x, y); // set new frame but with specified offset
}];
}
只是提示:将方法签名更改为此
-(void)moveButton:(UIButton*)button alongX:(CGFloat)x alongY:(CGFloat)y withTime:(CGFloat)time;
-(void)scaleButton:(UIButton*)button width:(CGFloat)width height:(CGFloat)height withTime:(CGFloat)time;
希望这是您的解决方案:)
Animator *anim = [[Animator alloc] init];
[anim moveButton:myButton alongX:-100 alongY:200 withTime:1.5];
答案 1 :(得分:0)
对于您提供的示例,我认为您实际上不需要通过引用传递按钮。 Animator
上的方法采用指向UIButton
和myButton
的指针。如果要重新指定指针,则只需要通过引用传递对象,Animator
当前不执行此操作。
我认为您只需将moveButton:&myButton
更改为moveButton:myButton
。
话虽如此,如果你曾经 希望通过引用传递指针(通常用NSError
指出params),你需要更改签名以使用双指针和像你一样使用&
运算符传递。
NSFileManager
remoteItemAtURL:error:
方法的签名就是一个例子。
NSError *error = nil;
[[NSFileManager defaultManager] removeItemAtURL:url error:&error];
// error could now be pointing to something other than nil