我想使用UIanimationtransitioncurl向右或向左使用动画来进行imageview。可能吗?

时间:2011-01-24 09:37:56

标签: iphone uiimageview uiviewanimation uiviewanimationtransition

我想像我的书页一样动画我的照片库。 有没有任何方法可以在左侧或右侧使用UIanimationtransitioncurl?

这是您向我建议的代码。我已经在viewdidload方法上完成了分配。

self.title = @"TransitionsTitle";

// Create the container view which we will use for transition animation (centered horizontally).
CGRect frame = CGRectMake(round((self.view.bounds.size.width - kImageWidth) / 2.0),
                                                    kTopPlacement, kImageWidth, kImageHeight);
self.containerView = [[[UIView alloc] initWithFrame:frame] autorelease];
[self.view addSubview:self.containerView];

// The container view can represent the images for accessibility.
[self.containerView setIsAccessibilityElement:YES];
[self.containerView setAccessibilityLabel:NSLocalizedString(@"ImagesTitle", @"")];

// Create the initial image view.
frame = CGRectMake(0.0, 0.0, kImageWidth, kImageHeight);
self.mainView = [[[UIImageView alloc] initWithFrame:frame] autorelease];
self.mainView.image = [UIImage imageNamed:@"scene1.jpg"];
[self.containerView addSubview:self.mainView];

// Create the alternate image view (to transition between).
CGRect imageFrame = CGRectMake(0.0, 0.0, kImageWidth, kImageHeight);
self.flipToView = [[[UIImageView alloc] initWithFrame:imageFrame] autorelease];
self.flipToView.image = [UIImage imageNamed:@"scene2.jpg"];
CGAffineTransform rotate=CGAffineTransformMakeRotation(3.14/2);
[containerView setTransform:rotate];
[self.view addSubview:containerView];
rotate=CGAffineTransformMakeRotation(-3.14/2);
[mainView setTransform:rotate];
[self.containerView addSubview:mainView];
[self.mainView addSubview:flipToView];

//我把它放在按钮动作事件上。

- (IBAction)flipAction:(id)sender{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:4];
    [UIView setAnimationTransition:([self.mainView superview] ?
                                        UIViewAnimationTransitionCurlDown : UIViewAnimationTransitionCurlDown)
                                        forView:self.containerView cache:YES];
    if ([flipToView superview])
    {
        [self.flipToView removeFromSuperview];
        [self.containerView addSubview:mainView];
    }
    else
    {
        [self.mainView removeFromSuperview];
        [self.containerView addSubview:flipToView];
    }
    [UIView commitAnimations];
}

1 个答案:

答案 0 :(得分:1)

是的,你可以。这是我用的代码,

    self.containerViewParent = [[[UIView alloc] initWithFrame:CGRectMake(what ever you want)] autorelease]; //Create a container parent for the container view.

    self.containerView = [[[UIView alloc] initWithFrame:GRectMake(what ever you want)] autorelease]; //Create a container view for the view you wish to display.

    CGAffineTransform rotate = CGAffineTransformMakeRotation(-3.14/2);
    [containerViewParent setTransform:rotate]; //Rotate the containerViewParent.

    [self.view addSubview:containerViewParent];//Add it to the main view.

    rotate = CGAffineTransformMakeRotation(3.14/2);
    [containerView setTransform:rotate];//Rotate the containerView.

    [self.containerViewParent addSubview:containerView]; //Add it to the self.containerViewParent.
    [self.containerView addSubview:viewToDisplay]; //Add the view you want to display.

然后将转换应用于父视图

    [UIView beginAnimations:@"page transition" context:nil];
    [UIView setAnimationDuration:2.0];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.containerView cache:YES];
    [UIView commitAnimations];

向上和向下卷曲时,它会向左或向右弯曲。

**请注意,您需要使用视图的偏移来使其正确显示, 但它对我很有用。