Anjan
Anjan

Reputation: 360

I want to place imageview at different position with animation

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];
   }

Upvotes: 0

Views: 304

Answers (1)

Tomasz Stanczak
Tomasz Stanczak

Reputation: 13164

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.

Upvotes: 1

Related Questions