I want to place imageview at different position wi

2020-05-03 10:28发布

问题:

I am trying to set multiple image on different position this is my code anyone help me to set imageview on different position.

- (void)viewDidLoad 
{   

    [super viewDidLoad];

    frameX = 45.0f;

    frameY = 60.0f;

    imagearr = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"King2.png"],
                [UIImage imageNamed:@"3.png"],[UIImage imageNamed:@"queen.png"],
                [UIImage imageNamed:@"King.png"],nil]; 

}
-(IBAction)hidebtn:(id)sender
{

  [btn setHidden:TRUE]; 
    [self setCards];

}

-(void)setCards
{
    CGFloat dx = self.view.frame.size.width/2;

    CGFloat dy = self.view.frame.size.height/2;

    [cards setAnimationImages:imagearr];

    CGRect frame = cards.frame;
    frame.size.width = frameX;
    frame.size.height = frameY;
    [cards setFrame:frame];
    cards.animationRepeatCount = 1;
   for (int i=0; i<5;i++) 
   {

       [UIView beginAnimations:@"Move" context:nil];

       [UIView setAnimationDuration:0.3];

       switch (i) 
       {
           - (void)viewDidLoad 
{   

    [super viewDidLoad];
    frameX = 45.0f;
    frameY = 60.0f;

    imagearr = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"King2.png"],
                [UIImage imageNamed:@"3.png"],[UIImage imageNamed:@"queen.png"],
                [UIImage imageNamed:@"King.png"],nil]; 

}
-(IBAction)hidebtn:(id)sender
{
    [btn setHidden:TRUE];   
    [self setCards];
}

-(void)setCards
{
    CGFloat dx = self.view.frame.size.width/2;
    CGFloat dy = self.view.frame.size.height/2;

    [cards setAnimationImages:imagearr];

    CGRect frame = cards.frame;
    frame.size.width = frameX;
    frame.size.height = frameY;
    [cards setFrame:frame];
    cards.animationRepeatCount = 1;

  for (int i=0; i<5;i++) 
   {
       [UIView beginAnimations:@"Move" context:nil];
       [UIView setAnimationDuration:0.3];
       switch (i) 
       {
           case 0:
                 [cards setCenter:CGPointMake(dx, dy-120.0)];
                 cards.transform = CGAffineTransformMakeRotation(0);  
                 break;
           default:
               break;
       }
       [self.view addSubview:cards];
       [UIView commitAnimations];
   }


                   default:
               break;
       }
       [self.view addSubview:cards];
       [UIView commitAnimations];
   }

回答1:

You are adding subviews too late, you can't animate what is not on screen. Add them before you start animations on some position then animate to the target position.