Подтвердить что ты не робот

Создание анимации Pop, похожее на представление UIAlertView

Я хотел бы представить представление так же, как и UIAlertView - pop/ spring. К сожалению, подклассификация UIAlertView не является вариантом для представления, которое мне нужно представить. Я написал какой-то код, но я не могу представить его реалистичным, как хотелось бы. Я был бы признателен за любые предложения по большей реалистичности или ссылке, если что-либо подобное было сделано (я ничего не нашел в Google). Спасибо.

  - (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];
}
4b9b3361

Ответ 1

- (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"];
}

Ответ 2

указатель на сообщение delackner - лучший исходный, который я нашел. я бы предложил только для людей, пытающихся реально имитировать UIAlertView несколько вещей:

  • добавление цвета округлых краев, границ и полупрозрачного слоя
  • некоторые альфа-fade-in/-out и
  • повторение с блоками как несколько более сжатое с последней технологической цепочкой 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;
                                                       }
                                       ];
                                    }
                    ];
                 }
 ];

Ответ 3

Одна вещь: многоступенчатые анимации, подобные этому, намного проще, если вы используете CAKeyframeAnimation вместо мутирования анимации в очереди UIView.