在Objective-C中创建自定义动画?

时间:2013-03-02 07:31:48

标签: ios

我想在iOS中创建自定义动画。 假设,我有一个用它的SubViews显示的UIView。我想把它的截图分成四个部分,应该是lefttop,righttop,bottomleft,从中间到底部,并将每个部分移动到它们的角落。由于lefttop应该向左移动UIView的右上角和右上角应该移动到右上角,依此类推。

2 个答案:

答案 0 :(得分:0)

可能是这个帮助你但不确定, 首先设置你的帧位置,然后改变帧的位置,无论你想要什么, 默认我正在服用

                 top.frame=CGRectMake(0,0,320,120);
                 bottom.frame=CGRectMake(0,315,320,145);
                 left.frame=CGRectMake(0,113,151,202);
                 right.frame=CGRectMake(175,113,151,202);

[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(tranform) userInfo:nil repeats:NO];

3秒后,此方法将调用,

-(void)tranform{
     [UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationTransitionCurlUp
                     animations:^{
                         top.frame=CGRectMake(0,-120,320,120);
                         bottom.frame=CGRectMake(0,480,320,145);
                         left.frame=CGRectMake(-151,113,151,202);
                         right.frame=CGRectMake(320,113,151,202);



                     }
                     completion:nil];
}

在上面的代码顶部,底部,左侧,右侧是四个视图,我将所有这些添加到self.view 并且当点击特定但是关于它们各自的帧时将发生动画。

答案 1 :(得分:0)

这是我的回答,它将截取当前屏幕显示的屏幕截图,将其从中心分成四个相等的部分,并将其动画移动到各自的角落。

- (IBAction)animate:(id)sender
{
    NSLog(@"animation called ");
    UIGraphicsBeginImageContext(self.view.window.bounds.size);
    [self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    CGImageRef imgref=[image CGImage];
    NSArray *arrayOfImages= [[NSArray alloc]initWithArray:[self splitImageIntoRects:imgref]];

    NSLog(@"array of images are %@",arrayOfImages);


    for (CALayer *layer in arrayOfImages) {
        [self.view.layer addSublayer:layer];
    }

    [self animateLayers:arrayOfImages];
}

-(void)animateLayers:(NSArray*)array
{
    for (int i=0;i<array.count;i++)
    {
        CALayer *layer=[array objectAtIndex:i];
        CGRect rect=[layer frame];
        CGPoint newPt;

        switch (i) {
            case 0:
                newPt=CGPointMake(-rect.size.width, -rect.size.height);
                break;
            case 1:
                newPt=CGPointMake(rect.origin.x-rect.size.width, rect.origin.y+rect.size.height);
                break;
            case 2:
                newPt=CGPointMake(rect.origin.x+rect.size.width, rect.origin.y-rect.size.height);
                break;
            case 3:
                newPt=CGPointMake(rect.origin.x+rect.size.width, rect.origin.y+rect.size.height);
                break;
        }
        [self moveLayer:layer to:newPt];
    }
}

-(void)moveLayer:(CALayer*)layer to:(CGPoint)point
{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    animation.duration=1;
    animation.fromValue = [layer valueForKey:@"position"];

    animation.toValue = [NSValue valueWithCGPoint:point];
    layer.position = point;
    [layer addAnimation:animation forKey:@"position"];
}

- (NSArray*)splitImageIntoRects:(CGImageRef)anImage{

    CGSize imageSize = CGSizeMake(CGImageGetWidth(anImage), CGImageGetHeight(anImage));

    NSMutableArray *splitLayers = [NSMutableArray array];

    int kXSlices = 2;
    int kYSlices = 2;

    for(int x = 0;x < kXSlices;x++) {
        for(int y = 0;y < kYSlices;y++) {
            CGRect frame = CGRectMake((imageSize.width / kXSlices) * x,
                                      (imageSize.height / kYSlices) * y,
                                      (imageSize.width / kXSlices),
                                      (imageSize.height / kYSlices));

            CALayer *layer = [CALayer layer];
            layer.frame = frame;
            CGImageRef subimage = CGImageCreateWithImageInRect(anImage, frame);
            layer.contents = (__bridge id)subimage;
            CFRelease(subimage);
            [splitLayers addObject:layer];
        }
    }

    return splitLayers;
}