2010-08-10 7 views

Respuesta

13
  • Cree su UIView y configurarlo (o cualquier subclase de los mismos tales como UIImageView)
  • Ajuste la posición de su imagen para estar donde el usuario está tocando:

Hay cuatro delegar métodos para aceptar eventos táctiles que son parte de cualquier clase que hereda de UIResponder, como UIView. Use el método delegado que sea más apropiado para usted. Si desea que siga el dedo, esto será -touchesMoved:

- (void) touchesMoved:(NSSet*)toucheswithEvent:(UIEvent*)event {   
    CGPoint pt = [[touches anyObject] locationInView:self]; 
    myImageView.center = pt; 
} 

Otros métodos de delegado disponibles para usted son:

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

me escribió un ejemplo de aplicación que hace exactamente lo que quiere. Es una demostración de dibujo de gráficos Quartz 2D, pero dibuja un cuadrado rojo y el círculo negro en el que arrastrar el dedo y debe ser lo suficientemente fácil de seguir:

alt text http://brockwoolf.com/shares/stackoverflow/3445494/screen.png

Download Xcode project (32kb)

+1

El enlace está inactivo, se puede actualizar por favor? Tengo exactamente el mismo problema ! – Seb

1

hay una publicación excelente aquí.

por Divan Visagie

Este es el código correspondiente (tomada desde el enlace de arriba):

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 

    //Find the path for the menu resource and load it into the menu array 
    NSString *menuPlistPath = [[NSBundle mainBundle] pathForResource:@"Menu" ofType:@"plist"]; 

    menuArray = [[NSArray alloc] initWithContentsOfFile:menuPlistPath]; 

    //add some gestures 
// UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeLeft:)]; 
// [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft]; 
    //[self.view addGestureRecognizer:swipeLeft]; 

// UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeRight:)]; 
// [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight]; 
    //[self.view addGestureRecognizer:swipeRight]; 

} 



float difference; 
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    CGPoint contentTouchPoint = [[touches anyObject] locationInView:content]; 
    difference = contentTouchPoint.x; 
} 


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

    CGPoint pointInView = [[touches anyObject] locationInView:self.view]; 

    float xTarget = pointInView.x - difference; 
    if(xTarget > menuTable.frame.size.width) 
     xTarget = menuTable.frame.size.width; 
    else if(xTarget < 0) 
     xTarget = 0; 

    [UIView animateWithDuration:.25 
        animations:^{ 

         [content setFrame:CGRectMake(xTarget, content.frame.origin.y, content.frame.size.width, content.frame.size.height)]; 
        } 
    ]; 
} 


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


    CGPoint endPoint = [[touches anyObject] locationInView:self.view]; 
    float xTarget = endPoint.x - difference; 
    if(xTarget < (menuTable.frame.size.width/2)) 
     xTarget = 0; 
    else 
     xTarget = menuTable.frame.size.width; 

    [UIView animateWithDuration:.25 
        animations:^{ 

         [content setFrame:CGRectMake(xTarget, content.frame.origin.y, content.frame.size.width, content.frame.size.height)]; 
        } 
    ]; 
} 
Cuestiones relacionadas