簡體   English   中英

對於iphone開發,如何翻轉UIImageView?

[英]For iphone development, how to flip a UIImageView?

我想翻轉一個imageView(左/右),但我找不到UIView(或UIImageView)方法來做到這一點? 任何想法?

謝謝!

我認為你唯一想要的是:

UIView的的

+ (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache;

UIViewAnimationTransitionFlipFromLeft,
UIViewAnimationTransitionFlipFromRight,

這些動畫過渡只能在動畫塊中使用。 在容器視圖上設置轉換,然后換出舊視圖以用於新視圖,然后提交動畫。

喜歡:

CGContextRef context = UIGraphicsGetCurrentContext();

[UIView beginAnimations:nil context:context];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:yourContainerView cache:YES];
[yourContainerView exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
[UIView commitAnimations];

將您的UIIMageView放入UIView並使用此代碼(來自實用程序app項目示例)

- (void)loadFlipsideViewController {

    FlipsideViewController *viewController = [[FlipsideViewController alloc] initWithNibName:@"FlipsideView" bundle:nil];
    self.flipsideViewController = viewController;
    [viewController release];

    // Set up the navigation bar
    UINavigationBar *aNavigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
    aNavigationBar.barStyle = UIBarStyleBlackOpaque;
    self.flipsideNavigationBar = aNavigationBar;
    [aNavigationBar release];

    UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(toggleView)];
    UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:@"Test123"];
    navigationItem.rightBarButtonItem = buttonItem;
    [flipsideNavigationBar pushNavigationItem:navigationItem animated:NO];
    [navigationItem release];
    [buttonItem release];
}


- (IBAction)toggleView {    
    /*
     This method is called when the info or Done button is pressed.
     It flips the displayed view from the main view to the flipside view and vice-versa.
     */
    if (flipsideViewController == nil) {
        [self loadFlipsideViewController];
    }

    UIView *mainView = mainViewController.view;
    UIView *flipsideView = flipsideViewController.view;

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1];
    [UIView setAnimationTransition:([mainView superview] ? UIViewAnimationTransitionFlipFromRight : UIViewAnimationTransitionFlipFromLeft) forView:self.view cache:YES];

    if ([mainView superview] != nil) {
        [flipsideViewController viewWillAppear:YES];
        [mainViewController viewWillDisappear:YES];
        [mainView removeFromSuperview];
        [infoButton removeFromSuperview];
        [self.view addSubview:flipsideView];
        [self.view insertSubview:flipsideNavigationBar aboveSubview:flipsideView];
        [mainViewController viewDidDisappear:YES];
        [flipsideViewController viewDidAppear:YES];

    } else {
        [mainViewController viewWillAppear:YES];
        [flipsideViewController viewWillDisappear:YES];
        [flipsideView removeFromSuperview];
        [flipsideNavigationBar removeFromSuperview];
        [self.view addSubview:mainView];
        [self.view insertSubview:infoButton aboveSubview:mainViewController.view];
        [flipsideViewController viewDidDisappear:YES];
        [mainViewController viewDidAppear:YES];
    }
    [UIView commitAnimations];
}

mainView和flipToView是imageViews的對象,而containerView是UIView的對象。

下面給出了兩個示例代碼,用於卷曲imageView並將ImageView從左向右翻轉

- (void)curlAction:(id)sender
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.75];

    [UIView setAnimationTransition:([mainView superview] ?                          UIViewAnimationTransitionCurlUp : UIViewAnimationTransitionCurlDown)forView:containerView cache:YES];
    if ([flipToView superview])
    {
        [flipToView removeFromSuperview];
        [containerView addSubview:mainView];
    }
    else
    {
        [mainView removeFromSuperview];
        [containerView addSubview:flipToView];
    }

    [UIView commitAnimations];
}

並且從左到右將圖像帶到這里是代碼

- (void)flipAction:(id)sender
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.75];

    [UIView setAnimationTransition:([mainView superview] ?
                                        UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromRight)
                                        forView:containerView cache:YES];
    if ([flipToView superview])
    {
        [flipToView removeFromSuperview];
        [containerView addSubview:mainView];
    }
    else
    {
        [mainView removeFromSuperview];
        [containerView addSubview:flipToView];
    }

    [UIView commitAnimations];
}

謝謝

另一個解決方案可以在我的回購中找到,但它不涉及動畫! 它僅操縱圖像方向以提供一些圖像效果,即垂直/水平翻轉並向左/向右旋轉90度。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM