0

アプリ全体でより多くの異なるタイミング関数をすばやく使用する方法があるかどうか疑問に思っています. たとえば、Robert Penner のを使用すると、多くのツールキットがそれらを公開しますが、Apple が公開しないことに驚いています。

基本的には、UIView の animateWithDuration:delay:options:animations:completion: と同じくらい柔軟なブロック ベースのコマンドを使用する方法です。

私はこの解決策を思いつきました。より良い代替案があるかどうかを知ることは素晴らしいことです.

//  UIView+CustomTimingFunction.h
#import <UIKit/UIKit.h>

typedef enum {
    CustomTimingFunctionDefault,
    CustomTimingFunctionLinear,
    CustomTimingFunctionEaseIn,
    CustomTimingFunctionEaseOut,
    ....
} CustomTimingFunction;

@interface UIView (CustomTimingFunction)

- (void)animateProperty:(NSString *)property from:(id)from to:(id)to withDuration:(NSTimeInterval)duration customTimingFunction:(CustomTimingFunction)customTimingFunction competion:(void (^)(void))competion;

@end

//  UIView+CustomTimingFunction.m

#import "UIView+CustomTimingFunction.h"

@implementation UIView (CustomTimingFunction)

- (void)animateProperty:(NSString *)property from:(id)from to:(id)to withDuration:(NSTimeInterval)duration customTimingFunction:(CustomTimingFunction)customTimingFunction competion:(void (^)(void))competion
{
    [CATransaction begin]; {
        [CATransaction setCompletionBlock:competion];

        if([property isEqualToString:@"frame"]){
            CGRect fromRect = [from CGRectValue];
            CGPoint fromPosition = CGPointMake(CGRectGetMidX(fromRect), CGRectGetMidY(fromRect));
            CGRect toRect = [to CGRectValue];
            CGPoint toPosition = CGPointMake(CGRectGetMidX(toRect), CGRectGetMidY(toRect));

            CABasicAnimation *positionAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
            positionAnimation.timingFunction = [[self class] functionWithType:customTimingFunction];
            positionAnimation.fromValue = [NSValue valueWithCGPoint:fromPosition];
            positionAnimation.toValue = [NSValue valueWithCGPoint:toPosition];

            CABasicAnimation *boundsAnimation = [CABasicAnimation animationWithKeyPath:@"bounds"];
            boundsAnimation.timingFunction = [[self class] functionWithType:customTimingFunction];
            boundsAnimation.fromValue = from;
            boundsAnimation.toValue = to;

            CAAnimationGroup * group =[CAAnimationGroup animation];
            group.removedOnCompletion=NO;
            group.fillMode=kCAFillModeForwards;
            group.animations = @[positionAnimation,boundsAnimation];
            group.duration = duration;

            [self.layer addAnimation:group forKey:property];
        }else{
            CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:property];
            animation.duration = duration;
            animation.timingFunction = [[self class] functionWithType:customTimingFunction];
            animation.fillMode = kCAFillModeForwards;
            animation.removedOnCompletion = NO;
            animation.fromValue = from;
            animation.toValue = to;
            [self.layer addAnimation:animation forKey:animation.keyPath];
        }
    } [CATransaction commit];

}

+(CAMediaTimingFunction *)functionWithType:(CustomTimingFunction)type
{
    switch (type) {
        case CustomTimingFunctionDefault:
            return [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
            break;
        case CustomTimingFunctionLinear:
            return [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
            break;

        case CustomTimingFunctionEaseIn:
            return [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
            break;
        case CustomTimingFunctionEaseOut:
            return [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
            break;
        case CustomTimingFunctionEaseInOut:
            return [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
            break;

        ....
    }

    NSLog(@"Couldn't find CustomTimingFunction, will return linear");
    return [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
}

@end

ありがとう!

4

0 に答える 0