1

在 apportable 的门户应用程序上,我需要通过调用制作 UIView *object 的一些动画(移动/缩放/更改 alpha):

[UIView
 animateWithDuration:1.f
 delay:0.5f
 options:UIViewAnimationOptionAllowUserInteraction
 animations:^(void)
 {
     myview.center    = moveTo;
     myview.transform = transformTo;
     myview.alpha     = alphaTo;
 }
 completion:^(BOOL finished)
 {
     [self animationFinished];
 }];

现在只是做延迟,然后立即执行动画代码和完成代码。

4

2 回答 2

3

谢谢你的答案。但我需要“今天”的动画,所以我下一节课。它的工作不是很好,但它比没有好得多。也许对一个人会有所帮助

AOTAnimate.h

//
//  AOTAnimate.h
//
//  Created by Andrei Bakulin on 18/11/2013.
//

#import <Foundation/Foundation.h>

@interface AOTAnimate : NSObject
{
    UIView          *view;

    NSInteger       animationTicksLeft;
    CGFloat         scaleX;
    CGFloat         scaleY;

    CGPoint         moveDelta;
    CGSize          scaleCurrent;
    CGSize          scaleDelta;
    CGFloat         alphaDelta;

    void (^completeAction)();
}

@property (nonatomic, assign)   CGFloat                 duration;
@property (nonatomic, assign)   CGFloat                 delay;
@property (nonatomic, assign)   CGFloat                 frequency;
@property (nonatomic, assign)   UIViewAnimationOptions  options;

@property (nonatomic, assign)   CGPoint                 moveFrom;
@property (nonatomic, assign)   CGAffineTransform       transformFrom;
@property (nonatomic, assign)   CGFloat                 alphaFrom;

@property (nonatomic, assign)   CGPoint                 moveTo;
@property (nonatomic, assign)   CGAffineTransform       transformTo;
@property (nonatomic, assign)   CGFloat                 alphaTo;

+ (AOTAnimate*)makeAnimationOnView:(UIView*)view_ duration:(CGFloat)duration_;
+ (AOTAnimate*)makeAnimationOnView:(UIView*)view_ duration:(CGFloat)duration_ delay:(CGFloat)delay_;

- (void)run;
- (void)runWithCompleteAction:(void (^)(void))complete_;

@end

AOTA动画.m

//
//  AOTAnimate.m
//
//  Created by Andrei Bakulin on 18/11/2013.
//

#import "AOTAnimate.h"

@implementation AOTAnimate

@synthesize duration, delay, frequency, options;
@synthesize moveFrom, transformFrom, alphaFrom;
@synthesize moveTo, transformTo, alphaTo;

+ (AOTAnimate*)makeAnimationOnView:(UIView*)view_ duration:(CGFloat)duration_
{
    return [self makeAnimationOnView:view_ duration:duration_ delay:0.f];
}

+ (AOTAnimate*)makeAnimationOnView:(UIView*)view_ duration:(CGFloat)duration_ delay:(CGFloat)delay_
{
    return [[AOTAnimate alloc] initWithView:view_ duration:(CGFloat)duration_ delay:(CGFloat)delay_];
}

//----------------------------------

- (void)dealloc
{
    [view release];

    if( completeAction )
        Block_release(completeAction);

    [super dealloc];
}

- (id)initWithView:(UIView*)view_ duration:(CGFloat)duration_ delay:(CGFloat)delay_
{
    self = [super init];
    if (self)
    {
        view            = [view_ retain];
        duration        = duration_;
        delay           = delay_;

        frequency       = 0.025f;
        options         = UIViewAnimationOptionAllowUserInteraction;

        moveFrom        = view.center;
        transformFrom   = view.transform;
        alphaFrom       = view.alpha;

        moveTo          = view.center;
        transformTo     = view.transform;
        alphaTo         = view.alpha;
    }
    return self;
}

//----------------------------------
#pragma mark - Run animation

- (void)run
{
    [self runWithCompleteAction:nil];
}

- (void)runWithCompleteAction:(void (^)(void))complete_
{
    view.center    = moveFrom;
    view.transform = transformFrom;
    view.alpha     = alphaFrom;

#ifndef ANDROID

    [UIView
     animateWithDuration:duration
     delay:delay
     options:options
     animations:^(void)
     {
         view.center    = moveTo;
         view.transform = transformTo;
         view.alpha     = alphaTo;
     }
     completion:^(BOOL finished)
     {
         if( complete_ )
             complete_();
     }];

#else

    if( duration <= 0.f )
    {
        [self doAnimationComplete];
        return;
    }

    animationTicksLeft = ceil( duration / frequency );

    if( animationTicksLeft == 0 )
    {
        [self doAnimationComplete];
        return;
    }

    moveDelta   = CGPointMake( (moveTo.x-moveFrom.x)/animationTicksLeft, (moveTo.y-moveFrom.y)/animationTicksLeft );
    alphaDelta  = (alphaTo-alphaFrom)/animationTicksLeft;

    CGSize scaleFrom = CGSizeMake( [self scaleX:transformFrom], [self scaleY:transformFrom] );
    CGSize scaleTo   = CGSizeMake( [self scaleX:transformTo],   [self scaleY:transformTo]   );

    scaleDelta  = CGSizeMake((scaleTo.width - scaleFrom.width)/animationTicksLeft,
                             (scaleTo.height - scaleFrom.height)/animationTicksLeft );

    scaleCurrent = scaleFrom;

    if( complete_ )
    {
        completeAction = Block_copy(complete_);
    }

    [self performSelector:@selector(doAnimationTick) withObject:nil afterDelay:delay];

#endif
}

//----------------------------------
#pragma mark - Manual animation

#ifdef ANDROID

- (void)doAnimationTick
{
    if( CGPointEqualToPoint( moveDelta, CGPointZero ) == NO )
    {
        view.center = CGPointMake( view.center.x + moveDelta.x, view.center.y + moveDelta.y );
    }

    if( CGSizeEqualToSize( scaleDelta, CGSizeZero) == NO )
    {
        view.transform = CGAffineTransformMakeScale( scaleCurrent.width, scaleCurrent.height );
        scaleCurrent.width  += scaleDelta.width;
        scaleCurrent.height += scaleDelta.height;
    }

    if( alphaDelta != 0.f )
    {
        view.alpha = view.alpha + alphaDelta;
    }

    // - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    animationTicksLeft--;

    if( animationTicksLeft > 0 )
    {
        [self performSelector:@selector(doAnimationTick) withObject:nil afterDelay:frequency];
    }
    else
    {
        [self doAnimationComplete];
    }
}

- (void)doAnimationComplete
{
    view.center    = moveTo;
    view.transform = transformTo;
    view.alpha     = alphaTo;

    if( completeAction )
        completeAction();
}

//----------------------------------
#pragma mark - Helpers

- (CGFloat)scaleX:(CGAffineTransform)t
{
    return sqrt(t.a * t.a + t.c * t.c);
}

- (CGFloat)scaleY:(CGAffineTransform)t
{
    return sqrt(t.b * t.b + t.d * t.d);
}

#endif

@end

像这样使用:

UIView *someview;

AOTAnimate *animate = [AOTAnimate makeAnimationOnView:someview duration:1.f delay:0.5f];

// allow to assign - animate.moveFrom / .tranfromFrom / alphaFrom properties,
// but by default they are copy from UIView* object

animate.moveTo      = CGPointMake( 100, 200 ); // new point where need to move
animate.transformTo = CGAffineTransformScale( CGAffineTransformIdentity, 1.5f, 1.5f );
animate.alphaTo     = 0.5f;

[animate runWithCompleteAction:^{
    NSLog(@"Animation done..);
}];

如果此方法将在 iOS 设备上运行 - 它将使用正常的 [UIView animateWithDuration:...] 方法

PS:这个类只会从一个中心点“移动”到另一个中心点。变换仅用于缩放对象(不移动)。我的 2 台测试设备上的 Alpha 不受支持,但可能在某些地方支持。

于 2013-11-18T05:44:30.420 回答
0

动画不适用于当前版本的 Apportable 的 UIKit。不过,我们在 UIKit 的下一个版本中提供了功能齐全的动画。一旦我们对质量和覆盖范围感到满意,我们将发布它。

于 2013-11-18T03:18:11.447 回答