开发者

How do I call this methods with a parameter in ObjC

I'm quiet new in ObjC and app programming. All I want to do is fade in an image. For that I did this:

-(IBAction)changeImg:(id)sender{
    myImgView.image = [UIImage imageNamed:@"image.png"];
    myImgView.alpha = 0;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    myImgView.alpha = 1;
    [UIView commitAnimations];
}

That works quiet good. But now I want to put the fade function into an own method and I don't know how to call that method:

-(IBAction)changeImg:(id)sender{
    myImgView.image = [UIImage imageNamed:@"image.pn开发者_如何学Gog"];
    [myImgView fadeIn:self];
}

-(void) fadeIn:(UIImageView *) imgFade{
    imgFade.alpha = 0;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    imgFade.alpha = 1;
    [UIView commitAnimations];
}

When I call the method like this I get a warning:"'UIImageView' may not respond to '-fadeIn:'"

Can anyone help me please?


Sorry but I don't get it to work. The warning stays. And when I do it like kuroutadori suggested:

...
self.alpha = 0;
...

I get an error: "request for member 'alpha' in something not a structure or union"


Put -changeImg: after -fadeIn:. Better yet, declare your private methods in a category at the top of the file:

@interface MyClass (PrivateMethods)
- (void)fadeIn: (UIImageView *)imgFade;
@end

You don't need to pass self around like that--every method you call on the same object will have the same self.


You are declaring fadeIn: on your current class (we would need more code to see which one it is) yet calling it on an UIImageView instance (in your case myImgView).

You should either declare it privately in your current class implementation file (as Jonathan Grynspan suggested). Or if you're extensively using this animation, put it into UIImageView itself through a category:

@interface UIImageView (MyFade)

- (void) fadeIn;

@end

and then implement it with

@implementation UIImageView (MyFade)

- (void) fadeIn
{
    self.alpha = 0;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    selfe.alpha = 1;
    [UIView commitAnimations];
}

@end


That warning usually comes up because there is no written declaration in the the interface file or .h file.

I assume that fadeIn is in myImgView class, so why are you passing self? Just make it [myImgView fadeIn]; and replace imgFade with self.

Code for source .m

@implementation MyClass
-(IBAction)changeImg:(id)sender{
    myImgView.image = [UIImage imageNamed:@"image.png"];
    [myImgView fadeIn];
}

-(void) fadeIn{
    self.alpha = 0;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    selfe.alpha = 1;
    [UIView commitAnimations];
}
@end

Add to your header .h

  - (void) fadeIn;

Or as someone said, put at the top of the source file .m before @implementation.

@interface MyClass ()
  - (void) fadeIn;
@end 

To make it private


You've got it backwards.

[self fadeIn:myImgView];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜