通过引用传递 UIButton
Pass UIButton by reference
我正在尝试创建一个 "helper class" 来为 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。
有什么方法可以通过引用传递UI按钮吗?
测试代码:
#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
改用这个:
[anim moveButton:myButton alongX:-100 alongY:200 withTime:1.5]; // no asterisk!
请注意,我相信您已经像这样初始化了 myButton:
UIButton *myButton = [[UIButton alloc] init];
是一个指针而不是对象本身。所以使用 &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];
对于您提供的示例,我认为您实际上不需要通过引用传递按钮。 Animator
上的方法采用指向 UIButton
的指针,myButton
匹配该指针。如果你想重新分配指针,你只需要通过引用传递一个对象,Animator
目前不这样做。
我认为您只需将 moveButton:&myButton
更改为 moveButton:myButton
。
话虽这么说,如果您 做过 想要通过引用传递指针(通常使用 NSError
输出参数),您需要将签名更改为使用双指针并像您所做的那样使用 &
运算符传入。
以NSFileManager
的remoteItemAtURL:error:
方法的签名为例。
NSError *error = nil;
[[NSFileManager defaultManager] removeItemAtURL:url error:&error];
// error could now be pointing to something other than nil
我正在尝试创建一个 "helper class" 来为 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。
有什么方法可以通过引用传递UI按钮吗?
测试代码:
#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
改用这个:
[anim moveButton:myButton alongX:-100 alongY:200 withTime:1.5]; // no asterisk!
请注意,我相信您已经像这样初始化了 myButton:
UIButton *myButton = [[UIButton alloc] init];
是一个指针而不是对象本身。所以使用 &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];
对于您提供的示例,我认为您实际上不需要通过引用传递按钮。 Animator
上的方法采用指向 UIButton
的指针,myButton
匹配该指针。如果你想重新分配指针,你只需要通过引用传递一个对象,Animator
目前不这样做。
我认为您只需将 moveButton:&myButton
更改为 moveButton:myButton
。
话虽这么说,如果您 做过 想要通过引用传递指针(通常使用 NSError
输出参数),您需要将签名更改为使用双指针并像您所做的那样使用 &
运算符传入。
以NSFileManager
的remoteItemAtURL:error:
方法的签名为例。
NSError *error = nil;
[[NSFileManager defaultManager] removeItemAtURL:url error:&error];
// error could now be pointing to something other than nil