Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 创建类似于UIAlertView演示的Pop动画_Iphone_Cocoa Touch_Core Animation - Fatal编程技术网

Iphone 创建类似于UIAlertView演示的Pop动画

Iphone 创建类似于UIAlertView演示的Pop动画,iphone,cocoa-touch,core-animation,Iphone,Cocoa Touch,Core Animation,我想以与UIAlertView相同的方式呈现一个视图—pop/spring。不幸的是,对UIAlertView进行子类化不是我需要呈现的视图的选项。我已经写了一些代码,但是我似乎不能像我希望的那样实现它。如果有类似的建议(我在谷歌上找不到任何东西),我将不胜感激。多谢各位 - (id)initWithFrame:(CGRect)frame { if ((self = [super initWithFrame:frame])) { self.backgroundCol

我想以与UIAlertView相同的方式呈现一个视图—pop/spring。不幸的是,对UIAlertView进行子类化不是我需要呈现的视图的选项。我已经写了一些代码,但是我似乎不能像我希望的那样实现它。如果有类似的建议(我在谷歌上找不到任何东西),我将不胜感激。多谢各位

  - (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {

        self.backgroundColor = [UIColor whiteColor];

        v = [[UIView alloc] initWithFrame:CGRectMake(140, 140, 60, 60)];
        v.backgroundColor = [UIColor blueColor];

        [self addSubview:v];
        [self animate];

    }
    return self;
}

- (void)animate {

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDuration:0.2];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(popStep1Complete)];
    v.frame = CGRectMake(90, 90, 140, 140);
    [UIView commitAnimations];
}

- (void)popStep1Complete {

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDuration:0.15];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(popStep2Complete)];
    v.frame = CGRectMake(110, 110, 100, 100);
    [UIView commitAnimations];
}

- (void)popStep2Complete {

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDuration:0.15];
    v.frame = CGRectMake(100, 100, 120, 120);
    [UIView commitAnimations];
}

有一件事:如果使用
CAKeyframeAnimation
而不是使用多个UIView队列动画,那么像这样的多步骤动画就容易多了。

指向delackner文章的指针也是我发现的最好的初始指针。我只为那些试图真正模仿我的人提供以下几点:

- (void) attachPopUpAnimation
{
    CAKeyframeAnimation *animation = [CAKeyframeAnimation
        animationWithKeyPath:@"transform"];

    CATransform3D scale1 = CATransform3DMakeScale(0.5, 0.5, 1);
    CATransform3D scale2 = CATransform3DMakeScale(1.2, 1.2, 1);
    CATransform3D scale3 = CATransform3DMakeScale(0.9, 0.9, 1);
    CATransform3D scale4 = CATransform3DMakeScale(1.0, 1.0, 1);

    NSArray *frameValues = [NSArray arrayWithObjects:
        [NSValue valueWithCATransform3D:scale1],
        [NSValue valueWithCATransform3D:scale2],
        [NSValue valueWithCATransform3D:scale3],
        [NSValue valueWithCATransform3D:scale4],
        nil];
    [animation setValues:frameValues];

    NSArray *frameTimes = [NSArray arrayWithObjects:
        [NSNumber numberWithFloat:0.0],
        [NSNumber numberWithFloat:0.5],
        [NSNumber numberWithFloat:0.9],
        [NSNumber numberWithFloat:1.0],
        nil];    
    [animation setKeyTimes:frameTimes];

    animation.fillMode = kCAFillModeForwards;
    animation.removedOnCompletion = NO;
    animation.duration = .2;

    [self.layer addAnimation:animation forKey:@"popup"];
}
  • 添加圆角边、边框和半透明层颜色
  • 一些alpha淡入/淡出,以及
  • 使用最新的iOS工具链以更简洁的方式使用块重做
  • 我还发现,他建议的最初1.1比例放大太大,在大多数情况下,1.05在视觉上似乎更正确
  • 代码:

    self.layer.cornerRadius = 10.0;
    [self.layer setMasksToBounds:YES];
    self.layer.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:0.60].CGColor;
    self.layer.borderColor = [UIColor whiteColor].CGColor;
    self.layer.borderWidth = 1.1;
    if (self.hidden == YES) { // swoop in if coming from hidden, otherwise pulse in-place
      self.transform = CGAffineTransformMakeScale(0.6, 0.6);
    }
    self.hidden = NO;
    [UIView animateWithDuration:0.2
                     animations:^{
                       self.transform = CGAffineTransformMakeScale(1.05, 1.05);
                       self.alpha = 0.8;
                     }
                     completion:^(BOOL finished){
                       [UIView animateWithDuration:1/15.0
                                        animations:^{
                                          self.transform = CGAffineTransformMakeScale(0.9, 0.9);
                                          self.alpha = 0.9;
                                        }
                                        completion:^(BOOL finished) {
                                          [UIView animateWithDuration:1/7.5
                                                           animations:^{
                                                             self.transform = CGAffineTransformIdentity;                                                             
                                                             self.alpha = 1.0;
                                                           }
                                           ];
                                        }
                        ];
                     }
     ];
    

    谢谢,但请删除您的答案并将其作为评论,因为此问题现在显示为已回答。不,我不会,抱歉。虽然我意识到我的答案不是你想听到的,只是部分解决方案,但我认为这是一个有效的答案,因为它比你发布的代码更好地解决问题。这是怎么回事?在“我的父视图”或“我的UIAlertView”?
    UIView
    的任何子类中。在本例中,自定义的
    UIAlertView
    -类。第二次动画没有触发时,设置nil key为我解决了这个问题:[self.view.layer addAnimation:animation-forKey:nil];