简体   繁体   English

transitionFromView:和翻转的奇怪行为。

[英]transitionFromView: and strange behavior with flip.

I have a image wall ( UIScrollView ) and in there I have a lot of UIImageView's . 我有一个图像墙( UIScrollView ),在那里我有很多UIImageView's

Here is my code: 这是我的代码:

for (ThumbPosterModel *tPoster in _thumbsPosterStack) {

    UIImageView *imageView = [[UIImageView alloc] initWithImage:tPoster.thumb];
    imageView.userInteractionEnabled = YES;
    imageView.frame = CGRectMake(i, imageView.frame.origin.y, imageView.frame.size.width, imageView.frame.size.height);

    [tPoster setTag:tag];
    [_posterTagArr addObject:(BasicPosterModel*)tPoster];

    imageView.tag = tag;
    tag++;
    [posterWallScrollView addSubview:imageView];

    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageDoubleTapped:)];
    doubleTap.numberOfTapsRequired = 2;
    [imageView addGestureRecognizer:doubleTap];
}

And Here is my IBAction: 这是我的IBAction:

-(IBAction)imageDoubleTapped:(id)sender {
    NSInteger selectTag = (((UIGestureRecognizer*)sender).view.tag);
    for (BasicPosterModel *bPoster in _posterTagArr) {
        if(bPoster.tag == selectTag) {
            [UIView transitionFromView:(UIImageView*)((UIGestureRecognizer*)sender).view
                                toView:mySecordView //let's say next ImageView. Doesn't matter
                              duration:1.0
                               options:UIViewAnimationOptionTransitionCrossDissolve  
                            completion:^(BOOL finished) {
                                // animation completed
                            }];

        }
    }
}

And when I use: 当我使用时:

UIViewAnimationOptionTransitionCrossDissolve this is effect ONLY on my image in ScrollView. UIViewAnimationOptionTransitionCrossDissolve只是我的滚动型图像效果。 When I use in this code UIViewAnimationOptionTransitionFlipFromTop this affect to my scrollView. 当我在这段代码中使用UIViewAnimationOptionTransitionFlipFromTop这会影响到我的scrollView。

How it's possible? 怎么可能?

Of course I want to my Animation effect only for single image. 当然我想要仅针对单个图像的动画效果。

Here's how the transition works conceptually: 以下是转换在概念上的工作原理:

  1. The system renders the current state of the parent view of the fromView to an off-screen buffer ("before" state) 系统将fromView的父视图的当前状态呈现为屏幕外缓冲区(“之前”状态)
  2. the fromView is removed from its parent view fromView将从其父视图中删除
  3. the toView is added to the parentView (can be nil, you're right about that; in that case nothing is added) toView被添加到parentView(可以是nil,你是对的;在这种情况下,没有添加任何内容)
  4. The system renders the parent view into an off-screen buffer ("after" state") 系统将父视图渲染到屏幕外缓冲区(“后”状态“)
  5. the system animates the transition of the parent view from the "before" state to the "after" state 系统将父视图从“之前”状态转换为“之后”状态的动画

In your case the parent view is the scroll view, so the whole scroll view is transitioned. 在您的情况下,父视图是滚动视图,因此整个滚动视图已转换。 Counter to your perception, that's the case even when using a cross dissolve. 与您的感知相反,即使使用交叉溶解也是如此。 It's just that most of the scrollview (the part that's not covered by fromView) is identical before and after, so it looks as if that part does not take part in the transition - but it does. 只是大多数的scrollview(未被fromView覆盖的部分)在之前和之后都是相同的,所以看起来好像那部分没有参与过渡 - 但确实如此。

If I understand correctly, your are arranging multiple stacks of image views horizontally in your scroll view, and whenever someone double taps on the "uppermost" image view in a stack, you want to "reveal" the image directly below it, without affecting the other stacks. 如果我理解正确,你在滚动视图中水平排列多个图像视图堆栈,并且每当有人双击堆栈中的“最上面”图像视图时,你想要“直接”显示它下面的图像,而不会影响其他堆栈。

If you add a container view to the scrollView for each of these stacks that is the same size as the stack, and put all the ImageViews for this stack into this container view instead of directly into the scroll view, it will work. 如果为每个与堆栈大小相同的堆栈向scrollView添加容器视图,并将此堆栈的所有ImageViews放入此容器视图中而不是直接放入滚动视图中,它将起作用。 On tap, just do a transition from the tapped view to nil as you originally did. 点击,只需按照您最初的操作从点击视图转换为nil。 The transition's size on the screen will be limited to the container view (roughly - curls and flips use some space beyond its frame). 屏幕上的过渡大小将仅限于容器视图(粗略地说 - 卷曲和翻转使用其框架之外的一些空间)。

Depending on your requirements, you might want to remove the stack's container view after the last image in the stack is removed. 根据您的要求,您可能希望在删除堆栈中的最后一个映像后删除堆栈的容器视图。

The reason is that UIViewAnimationOptionTransitionFlipFromTop is only availavle since iOS 5.0 原因是UIViewAnimationOptionTransitionFlipFromTop仅在iOS 5.0之后可用

From UIView Class Reference : 来自UIView类参考

UIViewAnimationOptionTransitionFlipFromTop UIViewAnimationOptionTransitionFlipFromTop
A transition that flips a view around its horizontal axis from top to bottom. 从顶部到底部围绕其水平轴翻转视图的过渡。 The top side of the view moves toward the front and the bottom side toward the back. 视图的顶侧朝向后侧向前侧和底侧移动。
Available in iOS 5.0 and later. 适用于iOS 5.0及更高版本。
Declared in UIView.h.` 在UIView.h中声明

So, in iOS 4.3 it may cause unpredictable behavior. 因此,在iOS 4.3中,它可能会导致不可预测的行为。

First off you need to have a toView listed in order to use transitionFromView properly. 首先,您需要列出一个toView才能正确使用transitionFromView

Here is Apple's example of how it is done: 以下是Apple关于如何完成的示例

[UIView transitionFromView:(displayingPrimary ? primaryView : secondaryView)
        toView:(displayingPrimary ? secondaryView : primaryView)
        duration:1.0
        options:(displayingPrimary ? UIViewAnimationOptionTransitionFlipFromRight :
                    UIViewAnimationOptionTransitionFlipFromLeft)
        completion:^(BOOL finished) {
            if (finished) {
                displayingPrimary = !displayingPrimary;
            }
    }];

I do also believe that transitionFromView is suppose to take in superView as an argument. 我也相信transitionFromView假设将superView作为参数。

Also, I think that is better practice to add your UImageView to a container UIView and perform the action on the container, rather than directly on the UIImageView. 另外,我认为将UImageView添加到容器UIView并在容器上执行操作更好,而不是直接在UIImageView上执行。 Because essentially you will need a "parent" UIView to animate, while you add and remove subViews durning the animation. 因为基本上你需要一个“父”UIView来制作动画,同时在动画中添加和删除子视图。

As Cory says, transitionFromView: toView: looks like it is intended to have a destination view. 正如Cory所说,transitionFromView:toView:看起来像是要有目的地视图。 If you've only want to fade out a view, why not use a method intended to just work on a single view, such as transitionView:duration:options:animations:completion. 如果您只想淡出视图,为什么不使用一个仅用于单个视图的方法,例如transitionView:duration:options:animations:completion。

The example from the documentation would appear to do exactly what you want. 文档中的示例似乎完全符合您的要求。

[UIView transitionWithView:containerView
       duration:0.2
       options:UIViewAnimationOptionTransitionFlipFromLeft
       animations:^{ [fromView removeFromSuperview]; [containerView addSubview:toView]; }
       completion:NULL];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM