2010-08-10 2 views

ответ

13
  • Создайте свой UIView и настроить его (или любой подкласс его соли, такие как UIImageView)
  • Установите положение вашего изображения, чтобы быть там, где пользователь прикасается:

Есть четыре делегировать методы принятия сенсорных событий, которые являются частью любого класса, который наследует от UIResponder, например UIView. Используйте метод делегата, который наиболее подходит вам. Если вы хотите, чтобы следовать вашим пальцем, это будет -touchesMoved:

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

Другие делегат методы доступны для вас:

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

Я написал пример приложения, который делает именно то, что вы хотите. Это демо рисунок Quartz 2D-графики, но он рисует красный квадрат и черный круг, где вы перетащить свой палец и должен быть достаточно просто следовать:

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

Download Xcode project (32kb)

+1

Ссылка вниз, вы можете обновить его, пожалуйста? У меня точно такая же проблема ! – Seb

0

яблоке Библиотека Sample Code поставляется с хорошо написанным примером, названным Touches. Он также демонстрирует новую функцию UIGestureRecognizers в iOS 4.0.

1

здесь отличная почта.

по Divan Visagie

здесь соответствующий код (из приведенной выше ссылке):

- (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)]; 
        } 
    ]; 
} 
Смежные вопросы