Changer la taille d'un bouton image dynamiquement impossible ?

yodarkyodark Membre
12:17 modifié dans API UIKit #1
Bonjour à  tous !

J'ai un petit problème, je voudrais modifier la taille et la position d'un bouton dynamiquement. Cela fonctionne sans problème lorsqu'un bouton n'a pas d'image background. Cependant lorsque que j'affecte une image en background seul la position change et pas la taille...

Comment faire pour changer la taille d'un bouton image ?

[UIView beginAnimations:nil context:self];<br />		[UIView setAnimationDuration:0.5];<br />		<br />		if (cardTag &gt; 2) detailedViewCard.frame = CGRectMake(4,7,207,290);<br />		else detailedViewCard.frame = CGRectMake(264,6,207,290);<br />		<br />		[UIView commitAnimations];<br />		<br />		UIImage * mImage = [[[UIImage alloc] initWithContentsOfFile: [mainBundle pathForResource:[rawSet_p1 objectAtIndex:cardTag] ofType:@&quot;png&quot;]] autorelease ];<br />		[detailedViewCard setBackgroundImage:mImage forState:0];<br />


Ce code marche seulement que l'image demandée n'existe pas

Réponses

  • Philippe49Philippe49 Membre
    12:17 modifié #2
    Avec cela ?

    UIImage *stretchableImage = [mImage stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:leftCapWidth];<br />	[detailedViewCard setBackgroundImage: stretchableImage forState:UIControlStateNormal];
    
  • yodarkyodark Membre
    12:17 modifié #3
    Que dois-je mettre dans topCapHeight ? J'ais mis ca:
    UIImage *stretchableImage = [mImage stretchableImageWithLeftCapWidth:mImage.size.width topCapHeight:mImage.size.height];
    


    Le problème mon boutton ne grandit toujours pas mais en plus l'image ne s'adapte plus a la taille de mon bouton. Le bouton ne contient que le coin haut gauche de l'image (image plus grande que le bouton)
  • Philippe49Philippe49 Membre
    12:17 modifié #4
    As-tu lu la doc de cette méthode ?

    eftCapWidth
    The horizontal end-cap size. (read-only)

    @property(nonatomic, readonly) NSInteger leftCapWidth
    Discussion
    End caps specify the portion of an image that should not be resized when an image is stretched. This technique is used to implement buttons and other resizable image-based interface elements. When a button with end caps is resized, the resizing occurs only in the middle of the button, in the region between the end caps. The end caps themselves keep their original size and appearance.

  • yodarkyodark Membre
    12:17 modifié #5
    Je ne comprends pas vraiment quelle valeur je dois mettre pour resizer toute l'image. dans tout les cas que j'ai essayé l'image ne s'adapte plus a la taille du boutton
  • Philippe49Philippe49 Membre
    12:17 modifié #6
    Bon je vais essayer pour voir ... dans l'après-midi
  • Philippe49Philippe49 Membre
    12:17 modifié #7
    Voici un code qui fait le resize, mais le rendu n'est pas terrible, et stretchableImage  fait de plus belles images mais déforme le contenu

    Personnellement, je ferais le resize sur l'image pour gérer l'antialiazing. Tu trouveras un code tout fait sur ce site : recherche sur resizedImageToSize
  • yodarkyodark Membre
    12:17 modifié #8
    Je crois que le problème est pas la.

    Mon image s'adapte parfaitement a la taille du bouton si je fais setFrame.

    Par contre si je met de l'animation les bouton adopte immédiatement la taille finale au lieu de grandir progressivement. Si en revanche je n'ai pas d'image le bouton grandit progressivement.
  • Philippe49Philippe49 Membre
    12:17 modifié #9
    Ah bon, c'est autre chose. Il s'agit d'une animation .. euh c'était pas clair sur ton premier post.
    Je regarde ...
  • Philippe49Philippe49 Membre
    12:17 modifié #10

    Tu as essayé une CABasicAnimation sur le layer du bouton ?
  • yodarkyodark Membre
    12:17 modifié #11
    Oui pardon en relisant le post c'est vrai que je n'ai pas été clair sur l'animation.
    J'ai encore un peu de peine avec CAAnimation j'ai pas très bien compris l'animation sur un layer je n'arrive pas vraiment a voir comment m'y prendre.

    Comment appliquer une animation sur un layer précis ?
  • Philippe49Philippe49 Membre
    avril 2009 modifié #12
    Avec une UIImageView cela marche au poil, par contre échec sur le bouton.
    -(IBAction) stretchIt:(id)sender {<br />	CGRect frame=initialFrame;<br />	frame.size=CGSizeMake(20.+random()%200,20.+random()%200);<br />	[UIView beginAnimations:nil context:NULL];<br />	[UIView setAnimationDuration:5.];<br />	imageView.frame = frame;<br />	[UIView commitAnimations];<br />}<br />
    



    J'essaie maintenant deux choses
    • Voir ce qui bloque le bouton dans une telle animation
    • Sinon, transformer cela en une CABasicAnimation sur la frame genre qui suit, mais cela ne marche pas  ... pour l'instant
    <br />-(void)awakeFromNib {<br />	initialFrame=imageView.frame;<br />	imageView.layer.anchorPoint=(CGPoint){0.,0.};<br />}<br />-(IBAction) stretchIt:(id)sender {<br />	initialFrame.size=CGSizeMake(20.+random()%200,20.+random()%200);<br />	CABasicAnimation * animation=[CABasicAnimation animationWithKeyPath:@&quot;bounds.size&quot;];<br />	animation.delegate=self;<br />	animation.toValue=[NSValue valueWithCGSize:initialFrame.size];	<br /><br />	[animation setDuration:2.0];<br />	[animation setTimingFunction: [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];<br />	[imageView.layer addAnimation:animation forKey:@&quot;bounds.size&quot;];<br />}<br /><br />- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag {<br />	imageView.frame=initialFrame;<br />}<br />
    


  • Philippe49Philippe49 Membre
    12:17 modifié #13
    Le fichier
  • Philippe49Philippe49 Membre
    12:17 modifié #14
    Bon j'ai trouvé une piste

    stretchableButton.autoresizingMask=UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

    Il semblerait que mettre une image en background verrouille l'autoresizingMask.




  • Philippe49Philippe49 Membre
    12:17 modifié #15
    Pas trouvé. Il te reste l'option du bidouillage. par exemple :
    • Avant de lancer l'animation, supprimer le bouton de sa superview, le remplacer par une UIImageView
    • Lancer l'animation sur l'imageView, en déclarant un delegate à  l'anim
    • Dans la delegate méthod animationDidStop: finished: remplacer le bouton à  la place de l'imageView


  • Philippe49Philippe49 Membre
    avril 2009 modifié #16
    Mieux faire ton bouton transparent "Custom" et associer l'image view. :

    <br />@interface stretchButtonAppDelegate : NSObject &lt;UIApplicationDelegate&gt; {<br />	UIWindow *window;<br />	UIButton * stretchableButton;<br />	UIImageView * bgButtonView;<br />}<br /><br />@property (nonatomic, retain) IBOutlet UIWindow *window;<br />@property (nonatomic, retain) IBOutlet	UIButton * stretchableButton;<br />@property (nonatomic, retain) UIImageView * bgButtonView;<br />-(IBAction) stretchIt:(id)sender ;<br />@end<br /><br />
    


    <br />-(void)awakeFromNib {<br />	buttonFrame=stretchableButton.frame;<br />	bgButtonView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@&quot;RLosange.png&quot;]];<br />	bgButtonView.frame= buttonFrame;<br />	[window insertSubview:bgButtonView belowSubview:stretchableButton];<br />}<br /><br />-(IBAction) stretchIt:(id)sender {<br />	buttonFrame.size=CGSizeMake(20.+random()%200,20.+random()%200);<br />	[UIView beginAnimations:nil context:NULL];<br />	[UIView setAnimationDuration:5.];<br />	stretchableButton.frame = button Frame;<br />	bgButtonView.frame=buttonFrame;<br />	[UIView commitAnimations];<br />}<br />
    

  • yodarkyodark Membre
    12:17 modifié #17
    parfait excellent merci beaucoup !
Connectez-vous ou Inscrivez-vous pour répondre.