0

I'm programming in XCode and just trying to get the basic "Create a box, and animate it from one set of coordinates to another", but I can't find anything online that is that simple. Can somebody explain this to me?

Ekta Padaliya
  • 5,743
  • 3
  • 39
  • 51
Mkz
  • 1
  • 2
  • 6

1 Answers1

0

Try this:

//Add the initial box
UIView* boxView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 60, 60)];
CAShapeLayer *boxLayer = [CAShapeLayer layer];

//set colors
[boxLayer setStrokeColor:[[UIColor redColor] CGColor]];
[boxLayer setFillColor:[[UIColor clearColor] CGColor]];
[boxLayer setPath:[[UIBezierPath bezierPathWithRect:boxView.bounds] CGPath]];
[boxView.layer addSublayer:boxLayer];
[self.view addSubview:boxView];

//Animate box
[UIView animateWithDuration:0.7 animations:^{
    [boxView setTransform:CGAffineTransformMakeTranslation(100, 100)];
} completion:^(BOOL finished) {

}];

EDIT:

If you want to move your object to different positions you can make it like this:

CGPoint p1 = CGPointMake(100, 100);
CGPoint p2 = CGPointMake(200, 200);
CGPoint p3 = CGPointMake(300, 300);
//Animate box
[UIView animateWithDuration:0.7 animations:^{
    CGRect oldFrame = boxView.frame;
    boxView.frame = CGRectMake(p1.x, p1.y, oldFrame.size.width, oldFrame.size.height);
} completion:^(BOOL finished) {
    [UIView animateWithDuration:0.7 animations:^{
        CGRect oldFrame = boxView.frame;
        boxView.frame = CGRectMake(p2.x, p2.y, oldFrame.size.width, oldFrame.size.height);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.7 animations:^{
            CGRect oldFrame = boxView.frame;
            boxView.frame = CGRectMake(p3.x, p3.y, oldFrame.size.width, oldFrame.size.height);
        } completion:^(BOOL finished) {

        }];
    }];
}];
ben
  • 900
  • 9
  • 17
  • I get a problem with the //animate box portion. It gives me a bad access error before it can load up. code=2 – Mkz Nov 18 '16 at 00:45
  • Ok, I'm able to use the UIView to transform to a point, but I'd like to define the start, point A, B, C, etc. Is that possible with CGAffineTransformMakeTranslation ? – Mkz Nov 18 '16 at 01:17
  • yeah like this `CGAffineTransformMakeTranslation(pointB.x-pointA.x, pointB.y-pointA.y)` , please accept my answer as the correct one if you are happy with the result – ben Nov 18 '16 at 08:34
  • I'll let you know how it goes. I'm trying to make multiple movements to different coordinates in an animated way. It seems like after I set my shape layer position, and then call the transformmaketranslation, it just moves from the transform coordinates to the 'setposition' location; even tho the set position is declared before the transform. I'll send you a sample of the code when I can later. – Mkz Nov 18 '16 at 18:03
  • I'll give that a try tomorrow. Thanks for the help – Mkz Nov 24 '16 at 06:00