2011-12-31 16 views
5

Tengo una uiview en la parte superior de la interfaz (debajo de la barra de estado) que muestra solo la parte inferior de la misma.Tocar y desplegar una vista

En realidad, quiero hacer que la uiview roja que se deslice hacia abajo se muestre completamente mediante arrastre, como el centro de notificaciones en el iOS nativo y no solo presionando un botón.

¿Qué debo usar para "tocar y desplegar" la vista para que se pueda mostrar por completo?

Example

Respuesta

3

Hacer una subclase de UIView.

Anular touchesBegan:withEvent y touchesMoved:withEvent.

En el touchesBegan quizás realice un cambio visual para que el usuario sepa que están tocando la vista. En el touchesMoved usar [[touches anyObject] locationInView:self] y [[touches anyObject] previousLocationInView:self]

para calcular la diferencia entre la posición actual y toque la última posición de contacto (detectar arrastre hacia abajo o arrastrar una copia de seguridad).

Luego, si tiene un dibujo personalizado, llame al [self setNeedsDisplay] para decirle a su vista que vuelva a dibujar en su método drawRect:(CGRect)rect.

Nota: esto asume que la vista no usa múltiples toques.

1

remito a mi respuesta en iPhone App: implementation of Drag and drop images in UIView

Sólo tiene que utilizar TouchesBegin y TouchesEnded métodos. En ese ejemplo, he mostrado cómo usar CGPoint, en lugar de eso, debe intentar usar setFrame o drawRect para su visualización.

Tan pronto como TouchesMoved método se llama usted tiene que utilizar setFrame o drawRect (no estoy seguro, pero que siempre funciona, sobre todo setFrame) también toman la altura de CGPoint.

+0

Will - (void) pan: (UIPanGestureRecognizer *) ¿gesto hace el trabajo? – Alby

+1

Sí, pero debes ser más cuidadoso mientras lo usas ...ya que necesita definir el control adecuado y otras cosas ... También es bueno ... pero si usa Touchesbegin y TouchesEnded ... entonces simplemente puede dar IBOutlets en Interface Builder ... – DShah

9

No es necesario encontrar una solución de arrastrar y soltar. Un UIScrollView puede hacerlo sin ninguna pérdida de rendimiento al escuchar toques.

pulldown

@interface PulldownView : UIScrollView 

@end 

@implementation PulldownView 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (!self) { 
     return self; 
    } 
    self.pagingEnabled = YES; 
    self.bounces = NO; 
    self.showsVerticalScrollIndicator = NO; 
    [self setBackgroundColor:[UIColor clearColor]]; 
    double pixelsOutside = 20;// How many pixels left outside. 
    self.contentSize = CGSizeMake(320, frame.size.height * 2 - pixelsOutside); 
    // redArea is the draggable area in red. 
    UIView *redArea = [[UIView alloc] initWithFrame:frame]; 
    redArea.backgroundColor = [UIColor redColor]; 
    [self addSubview:redArea]; 
    return self; 
} 

// What this method does is to make sure that the user can only drag the view from inside the area in red. 
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event 
{ 
    if (point.y > height) 
    { 
     // Leaving useless touches to the views under it. 
     return nil; 
    } 
    return [super hitTest:point withEvent:event]; 
} 

@end 

Modo de empleo: 1. Inicializar
una instancia de PulldownView.
2. Agregue cualquier contenido que desee mostrar a la instancia utilizando [addSubview:].
3. Oculta el área en rojo.

[pulldownView setContentOffset:CGPointMake(0, heightOfTheView - pixelsOutside)]; 

Este es un ejemplo simple. Puede agregarle funciones como agregar una barra de botones titulada en la parte inferior del área que se puede arrastrar para implementar click-n-drop, o agregar algún método a la interfaz para reposicionarlo por la persona que llama.

+0

Esto es fantástico, exactamente lo que Lo necesitaba. ¡Gracias! – bcattle

Cuestiones relacionadas