2010-04-22 16 views
23

Vorrei presentare una visualizzazione nello stesso modo di UIAlertView - un pop/spring. Purtroppo la sottoclasse di UIAlertView non è un'opzione per la vista che devo presentare. Ho scritto del codice, ma non riesco a ottenerlo in modo realistico come vorrei. Gradirei qualsiasi suggerimento per un maggiore realismo o un link se qualcosa di simile è stato fatto (non ho trovato nulla su Google). Grazie.Creazione di un'animazione pop simile alla presentazione di 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]; 
} 

risposta

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

Dove va? Nella mia vista genitore o nel mio UIAlertView? – Moshe

+0

Qualsiasi sottoclasse di 'UIView'. In questo caso la classe 'UIAlertView' personalizzata. – zoul

+0

La seconda volta che l'animazione non è stata attivata, l'impostazione di nil key ha risolto il problema per me: [self.view.layer addAnimation: animation forKey: nil]; – amleszk

5

Una cosa: animazioni multi-step come questo sono molto più facile se si utilizza un CAKeyframeAnimation invece di mutiple UIView coda animazioni.

+3

Grazie, ma vorresti cancellare la tua risposta e includerla come commento perché questa domanda ora appare come risposta. – RunLoop

+9

No, non lo farò, mi dispiace. Mentre mi rendo conto che la mia risposta non è ciò che volevi sentire ed è solo una soluzione parziale, è IMO una risposta valida perché è un approccio molto migliore al problema rispetto al codice che hai postato. –

7

il puntatore del post di Delackner è il migliore iniziale che ho trovato. avrei offrono solo per le persone che cercano di imitare realmente UIAlertView alcune cose:

  1. aggiungendo i bordi arrotondati, bordo e semi-trasparente strato di colore
  2. alcuni alfa fade-in/out privati, e
  3. rifacendo con i blocchi come un po 'più succinta con le ultime iOS toolchain
  4. Ho anche trovato l'iniziale 1.1 scale-out suggerisce troppo grande, 1.05 sembrava più corretto visivamente a me nella maggior parte dei casi

Codice:

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; 
                 } 
             ]; 
            } 
        ]; 
       } 
]; 
Problemi correlati