Swr79
Swr79

Reputation: 75

Change image position with Button

I would want to change the position of an image in three different positions using a button... With my code the image is only moved of a position...

ViewController.h

@property (weak, nonatomic) IBOutlet UIImageView *Switch3Way;

- (IBAction)Switch3WayPressed:(id)sender;

ViewController.m

- (void)Switch3WayPressed:(id)sender {
CGRect frame = Switch3Way.frame;
frame.origin.x = 323;
frame.origin.y = 262;
Switch3Way.frame = frame;

}

Upvotes: 1

Views: 603

Answers (2)

Ivan Lesko
Ivan Lesko

Reputation: 1304

The following code assumes you want to animate your Switch3Way UIImageView IBOutlet property. This code snippet will move your UIImageView to three different locations and stop the animation at the last position.

#import <QuartzCore/QuartzCore.h>    

-(IBAction)move:(id)sender
{
    CGPoint firstPosition = CGPointMake(someXvalue, someYvalue);
    CGPoint secondPosition = CGPointMake(someXvalue, someYvalue);
    CGPoint thirdPosition  = CGPointMake(someXvalue, someYvalue);

    CABasicAnimation *posOne = [CABasicAnimation animationWithKeyPath:@"position"];
    posOne.fromValue = [NSValue valueWithCGPoint:_Switch3Way.layer.position];
    posOne.toValue   = [NSValue valueWithCGPoint:firstPosition];
    posOne.beginTime = 0;
    posOne.duration  = 1;

    CABasicAnimation *posTwo = [CABasicAnimation animationWithKeyPath:@"position"];
    posTwo.fromValue = [NSValue valueWithCGPoint:firstPosition];
    posTwo.toValue   = [NSValue valueWithCGPoint:secondPosition];
    posTwo.beginTime = 1;
    posTwo.duration  = 1;

    CABasicAnimation *posThree = [CABasicAnimation animationWithKeyPath:@"position"];
    posThree.fromValue = [NSValue valueWithCGPoint:secondPosition];
    posThree.toValue   = [NSValue valueWithCGPoint:thirdPosition];
    posThree.beginTime = 2;
    posThree.duration  = 1;

    CAAnimationGroup *anims = [CAAnimationGroup animation];
    anims.animations = [NSArray arrayWithObjects:posOne, posTwo, posThree, nil];
    anims.duration = 3;
    anims.fillMode = kCAFillModeForwards;
    anims.removedOnCompletion = NO;

    [_Switch3Way.layer addAnimation:anims forKey:nil];

    _Switch3Way.layer.position = thirdPosition;
}

Invasivecode has a pretty good series of tutorials on creating animations like you are referring to http://weblog.invasivecode.com/post/4448661320/core-animation-part-iii-basic-animations You will eventually want to use CAKeyframeAnimation objects to create these types of animations, but understanding CABasicAnimations is a good way to start creating animations with CoreAnimation.

Upvotes: 1

Subzero
Subzero

Reputation: 961

Can you specify the requirement as to why you need to move the button to three different places? Anyways, I hope you will do the change based on some logic- so define three enums in the header file, e.g. something like the following:

typedef enum {
buttonState1,
buttonState2,
buttonState3
} buttonState;

Then, as per your business logic requirement, set these enum variables at the proper place in your code, e.g. something like the following:

-(void)setButtonState{

buttonState = buttonState1;

}

Now, in your button touch-up-inside handler routine, use a switch statement to set the appropriate frame, e.g. something like the following:

- (void)Switch3WayPressed:(id)sender {
    if (![sender isKindOfClass:[Switch3Way class]])
    return;
 switch (buttonState)

 {
 case buttonState1:
      {
      CGRect frame = Switch3Way.frame;
      frame.origin.x = 323;
      frame.origin.y = 262;
      Switch3Way.frame = frame;
      break;
   }
 case buttonState2:
      //some other rect origin based on your logic
      break;
 case buttonState3:
      //some other rect origin based on your logic
      break;
 default:
      break;

 }

Upvotes: 1

Related Questions