Академический Документы
Профессиональный Документы
Культура Документы
ver: 110.216
1
UIView
Qu es una UIView?
Clase base de las views usadas para crear un GUI. El GUI se forma como una jerarqua (rbol) de views Propiedades para recorrer el rbol:
Construir el GUI
La jerarqua de views puede construirse con el Interface Builder Y mediante cdigo
- (id) initWithFrame:(CGRect)aRect; - (void) addSubview:(UIView*)aView; - (void) removeFromSuperview; - (void) insertSubview:(UIView*)aView atIndex:(int)index; - (void) insertSubview:(UIView*)aView belowSubview:(UIView*)otherView; - (void) insertSubview:(UIView*)aView aboveSubview:(UIView*)otherView;
El contenido de la View
La view dene un rea rectangular en el que se puede dibujar. Si el contenido sobrepasa el rectngulo, se puede pintar o no (clipToBounds). Se puede controlar su transparencia (alpha). La view entera se puede ocultar (hidden). Se puede escalar el contenido, aplicar transformaciones anes, etc.
Ver la documentacin
Eventos
Podemos gestionar los eventos que ocurren en ella. Se puede congurar para:
ignorar los eventos del usuario. decidir si soportar multi-touch. establecer reconocedores de gestos. bloquear el envio de eventos a otras views. etc.
7
Gestin de Memoria
Al aadir una view a la jerarqua se retiene. y el sacarla de la jerarqua se llama a release.
IBOutlet
Si una view es un IBOutlet:
La variable de instancia que la apunta la retiene @property (retain) IBOutlet UILabel * l; Paraliberarla, llamamos a release y apuntamos a nil. self.l = nil; Ejemplo: las UIViewController sin memoria lo hacen en viewDidUnload.
Y sobreescribimos dealloc para llamar a release. - (void) dealloc { [l release]; [super dealloc]; }
Coordenadas
10
Coordenadas
Origen arriba a la izquierda Las unidades son puntos, no pixeles. - Ajuste de resolucin es automtico. Propiedades: - CGRect bounds El origen y tamao de la view en su sistema de coordenadas - CGPoint center El centro de la view en el sistema de coordenadas de la view padre. - CGRect frame Rectngulo que contiene a la view en el sistema de coordenadas de su view padre (puede estar girada)
12
(90,25)
160
10 0
160
(170,105)
12 5
view padre
ew vi hi ja
UIView: Dibujar
14
- Quartz 2D
es parte del framework Core Graphics
- OpenGL ES
Slo veremos Quartz 2D.
15
El pintado perezoso - Nunca llamaremos directamente a drawRect - Cuando haya que refrescar, llamaremos a: -(void)setNeedsDisplay;
16
Temas a Tratar
contexto grco transformaciones paths colores fonts operaciones de pintado texto imgenes
17
Contexto Grco
El pintado se hace usando un contexto grco. Se obtiene usando el mtodo:
(CGContextRef)UIGraphicsGetCurrentContext();
- realizar las modicaciones que se deseen en el contexto grco - y recuperar el estado inicial con:
UIGraphicsPopContext(); Ejemplo de uso: - drawRect invoca un mtodo auxiliar que cambia el estado del contexto para hacer ciertos dibujos. - Este mtodo hace un push al principio y un pop al nal para que los cambios del estado sean locales a l.
19
- (void) drawAxis { ! CGContextRef context = UIGraphicsGetCurrentContext(); // Guardar el contexto actual UIGraphicsPushContext(context); // Realizo cambios en el estado del contexto CGContextSetLineWidth(context, 1); CGContextSetStrokeColorWithColor(context,[UIColor redColor].CGColor); . . . // Recupero el contexto inicial UIGraphicsPopContext(); } . . . - (void)drawRect:(CGRect)rect { [self drawAxis]; [self drawObject]; }
20
21
UIKits Envoltorios
[[UIColor blueColor] set];
UIRectFill(rect); UIRectFrame(rect);
22
Dibujar Texto
NSString *text = @hola; CGPoint pos = CGPointMake(10,10); UIFont *font = [UIFont systemFontOfSize:14];
23
Dibujar Imgenes
UIImage *img = [UIImage imageNamed:@"fondo.png"];
24
UIView: Animaciones
25
Aminaciones
UIView soporta animaciones al cambiar el valor de algunas propiedades: - frame, transform, alpha, hidden, La animacin se dene usando un mtodo de clase y bloques. - El mtodo de clase tiene parmetros para ajustar la animacin: retrasos, duracin, curva de velocidad, ... - El bloque contiene el cdigo que cambia el valor de las propiedades de la UIView. - Puede existir un Completion Block que se ejecuta al terminar la animacin. Aunque la animacin no haya terminado, el cambio en los valores de las propiedades es instantneo.
26
+ (void)animateWithDuration:(NSTimeInterval) animations:(void (^)(void)) + (void)animateWithDuration:(NSTimeInterval) animations:(void (^)(void)) completion:(void (^)(BOOL finished)) + (void)animateWithDuration:(NSTimeInterval) delay:(NSTimeInterval) options:(UIViewAnimationOptions) animations:(void (^)(void)) completion:(void (^)(BOOL finished))
27
Ejemplo
@interface AnimacionViewController : UIViewController { IBOutlet UILabel *label; } @property (retain) UILabel *label; -(IBAction) anima; @end
28
- (IBAction) anima { ! static int pos = 100; ! pos = 300-pos; ! CGPoint p = CGPointMake(pos, pos); ! [UIView animateWithDuration:1 animations:^{label.center = p;}]; }
29
Transiciones
Tambin se pueden animar los cambios en la jerarqua de views, y los cambios de visibilidad. + (void)transitionFromView:(UIView *) toView:(UIView *) duration:(NSTimeInterval) options:(UIViewAnimationOptions) completion:(void (^)(BOOL finished)) + (void)transitionWithView:(UIView *) duration:(NSTimeInterval) options:(UIViewAnimationOptions) animations:(void (^)(void)) completion:(void (^)(BOOL finished))
30
Otra forma
Las animaciones y trasiciones tambin pueden hacerse sin usar bloques Usando slo mtodos de clase
31
@implementation AnimacionViewController @synthesize label; -(IBAction) anima { static int pos = 100; [UIView beginAnimations:@payapaca context:nil]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [UIView setAnimationDuration:4]; pos = 300-pos; label.center = CGPointMake(pos, pos); [UIView commitAnimations]; } . . . @end
32