2011-08-06 13 views

Respuesta

24

Animar de la vista a transformar el uso de la animación UIView (using blocks o older api)

de cierto tamaño muy pequeño (como view.transform = CGAffineTransformMakeScale(0.1, 0.1)) a algo un poco más grande, entonces usted quiere que sea (como view.transform = CGAffineTransformMakeScale(1.1, 1.1)), a continuación, volver al tamaño deseado .. (view.transform = CGAffineTransformMakeScale(0.1, 0.1)), o añadir más pasos de rebote grande

y para moverla de un lado, poner en práctica el touch methods y cambiar el marco de la vista como el dedo se mueve

Editar: aquí está la código de muestra para UIView similar a UIAlertView.

MyAlertView.h:

#import <UIKit/UIKit.h> 

@interface MyAlertView : UIView { 
    CGPoint lastTouchLocation; 
    CGRect originalFrame; 
    BOOL isShown; 
} 

@property (nonatomic) BOOL isShown; 

- (void)show; 
- (void)hide; 

@end 

MyAlertView.m:

#import "MyAlertView.h" 

@implementation MyAlertView 

@synthesize isShown; 

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

     self.alpha = 0; 
     self.backgroundColor = [UIColor whiteColor]; 

     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 20)]; 
     label.text = @"Hellooooo!"; 
     label.textAlignment = UITextAlignmentCenter; 
     label.backgroundColor = [UIColor clearColor]; 
     [self addSubview:label]; 
     [label release]; 

     UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     closeButton.frame = CGRectMake(10, frame.size.height - 45, frame.size.width - 20, 35); 
     [closeButton setTitle:@"Close" forState:UIControlStateNormal]; 
     [closeButton addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside]; 
     [self addSubview:closeButton]; 
    } 
    return self; 
} 


#pragma mark Custom alert methods 

- (void)show 
{ 
    NSLog(@"show"); 
    isShown = YES; 
    self.transform = CGAffineTransformMakeScale(0.1, 0.1); 
    self.alpha = 0; 
    [UIView beginAnimations:@"showAlert" context:nil]; 
    [UIView setAnimationDelegate:self]; 
    self.transform = CGAffineTransformMakeScale(1.1, 1.1); 
    self.alpha = 1; 
    [UIView commitAnimations]; 
} 

- (void)hide 
{ 
    NSLog(@"hide"); 
    isShown = NO; 
    [UIView beginAnimations:@"hideAlert" context:nil]; 
    [UIView setAnimationDelegate:self]; 
    self.transform = CGAffineTransformMakeScale(0.1, 0.1); 
    self.alpha = 0; 
    [UIView commitAnimations]; 
} 

- (void)toggle 
{ 
    if (isShown) { 
     [self hide]; 
    } else { 
     [self show]; 
    } 
} 

#pragma mark Animation delegate 

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context 
{ 
    if ([animationID isEqualToString:@"showAlert"]) { 
     if (finished) { 
      [UIView beginAnimations:nil context:nil]; 
      self.transform = CGAffineTransformMakeScale(1.0, 1.0); 
      [UIView commitAnimations]; 
     } 
    } else if ([animationID isEqualToString:@"hideAlert"]) { 
     if (finished) { 
      self.transform = CGAffineTransformMakeScale(1.0, 1.0); 
      self.frame = originalFrame; 
     } 
    } 
} 

#pragma mark Touch methods 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    lastTouchLocation = [touch locationInView:self]; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint newTouchLocation = [touch locationInView:self]; 
    CGRect currentFrame = self.frame; 

    CGFloat deltaX = lastTouchLocation.x - newTouchLocation.x; 
    CGFloat deltaY = lastTouchLocation.y - newTouchLocation.y; 

    self.frame = CGRectMake(currentFrame.origin.x - deltaX, currentFrame.origin.y - deltaY, currentFrame.size.width, currentFrame.size.height); 
    lastTouchLocation = [touch locationInView:self]; 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

} 

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

} 

@end 

A continuación, en la que desea mostrar que alerta, necesita:

#import "MyAlertView.h" 

y:

MyAlertView *alert = [[MyAlertView alloc] initWithFrame:CGRectMake(20, 100, 280, 100)]; 
[viewFromWhichYouWillShowTheAlert addSubview:alert]; 
[alert release]; 

entonces muestran que el uso de [alert show];, ocultar usando [alert hide];, o alternar el uso de [alert toggle];

También puede moverlo cuando toque y arrastre (en cualquier lugar excepto en el botón de cierre). Espero que esto sea suficiente para comenzar. Si necesita una explicación para cualquier parte del código, solo pregunte.

Ah, y el aviso fijo el color de este objeto de blanco así que si usted muestra en la parte superior de otro punto de vista blanco, se le realmente no verlo, por lo que sólo cambia el color de fondo de cualquier punto de vista :)

0

puede adquirir ese simplemente siguiendo los pasos siguientes

  1. Crear UIView (viewa) de tamaño 320 * 480, por lo que cubrirá toda la pantalla del iPhone con el fondo establecido en clearColor.This servirá como punto de vista de súper nuestro propósito;
  2. Cree otra UIView (ViewB) de tamaño 320 * 480 con color de fondo en negro y opacidad en 40%. 3.Ahora puede agregar cualquier vista en ViewB.
  3. Ahora agregue ViewB a ViewA.

Finalmente puede presentar esta vista donde sea necesario. El efecto será, ViewA cubrirá el Background viewController, ViewB servirá como efecto de supresión para el controlador de vista de fondo y las vistas de B son el UIElement que verá.

Para efectos de animación, puede usar un código de animación básico en el UIElement en ViewB.