Animating a UIImage or UIImageView?

前端 未结 6 655
刺人心
刺人心 2021-02-09 06:18

I\'m trying to animate an image\'s height (from height of 0 pixels to 480 pixels) to create the effect of the image being rendered top-down.

With an UIImageView i notice

相关标签:
6条回答
  • 2021-02-09 06:23

    You may want to check out Animation Blocks. They're documented quite thoroughly here:

    http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/AnimatingViews/AnimatingViews.html

    All the code there is relevant, because UIImageView is a subclass of UIView.

    Happy coding.

    0 讨论(0)
  • 2021-02-09 06:30

    You should have a look on Flip Clock for iPad tutorial. It is with core animation, hence has a performance bonus.

    0 讨论(0)
  • 2021-02-09 06:38

    As mentioned in the original post, here is the answer i seem to have stumbled upon:

    For the top-most imageview, i set the content mode to Top (so it doesnt scale). Then in code, i set the clipsToBounds to True. Now i am able to animate the top-most imageview height thus giving me the effect i am looking for.

    0 讨论(0)
  • 2021-02-09 06:38

    Try set your UIImageView's contentMode property to one of the following value. I think what you need is UIViewContentModeScaleAspectFit:

    typedef enum {
        UIViewContentModeScaleToFill,
        UIViewContentModeScaleAspectFit,      // contents scaled to fit with fixed aspect. remainder is transparent
        UIViewContentModeScaleAspectFill,     // contents scaled to fill with fixed aspect. some portion of content may be clipped.
        UIViewContentModeRedraw,              // redraw on bounds change (calls -setNeedsDisplay)
        UIViewContentModeCenter,              // contents remain same size. positioned adjusted.
        UIViewContentModeTop,
        UIViewContentModeBottom,
        UIViewContentModeLeft,
        UIViewContentModeRight,
        UIViewContentModeTopLeft,
        UIViewContentModeTopRight,
        UIViewContentModeBottomLeft,
        UIViewContentModeBottomRight,
    } UIViewContentMode;
    
    0 讨论(0)
  • 2021-02-09 06:40

    Just a suggestion, assume you have two images: A and X (the X-ray image):

    +-------+               +-------+
    |       |               |       |
    |       |               |       |
    |   A   |               |   X   |
    |       |               |       |
    |       |               |       |
    +-------+               +-------+
    

    Dynamically you can create a one-pixel high image (temp), copy the content from X partially, put it on top of A:

    +-------+   +-------+   +-------+
    |       |   | temp1 | < |       |
    |       |   +-------+   |       |
    |   A   |               |   X   |
    |       |               |       |
    |       |               |       |
    +-------+               +-------+
    

    In a loop, dynamically create/resize the temp image, and copy more content from X:

    +-------+   +-------+   +-------+
    |       |   | temp2 | < |       |
    |       |   |       |   |       |
    |   A   |   +-------+   |   X   |
    |       |               |       |
    |       |               |       |
    +-------+               +-------+
    
    +-------+   +-------+   +-------+
    |       |   | temp3 | < |       |
    |       |   |       |   |       |
    |   A   |   |       |   |   X   |
    |       |   +-------+   |       |
    |       |               |       |
    +-------+               +-------+
    +-------+   +-------+   +-------+
    |       |   | temp4 | < |       |
    |       |   |       |   |       |
    |   A   |   |       |   |   X   |
    |       |   |       |   |       |
    |       |   +-------+   |       |
    +-------+               +-------+
    
    +-------+   +-------+   +-------+
    |       |   | tempX | < |       |
    |       |   |       |   |       |
    |   A   |   |       |   |   X   |
    |       |   |       |   |       |
    |       |   |       |   |       |
    +-------+   +-------+   +-------+
    
    0 讨论(0)
  • 2021-02-09 06:43

    Use an animation block like so:

    imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 0)];
    imageView.image = ...;
    [imageView setClipsToBounds:YES];
    [imageView setContentMode:UIViewContentModeTop];
    
    [self.view addSubview:imageView];
    [imageView release];
    
    [UIView animateWithDuration:4.0f
                          delay:1.0f
                        options:UIViewAnimationOptionCurveEaseIn
                     animations:^(void) {
                         imageView.frame = CGRectMake(0, 0, 320, 480);
                     }
                     completion:NULL];
    
    0 讨论(0)
提交回复
热议问题