2010-08-10 25 views

Trả lời

13
  • Tạo UIView của bạn và cấu hình nó (hoặc bất kỳ lớp con của chúng như một UIImageView)
  • Đặt vị trí của hình ảnh của bạn trở thành nơi người dùng đang chạm vào:

Bốn phương thức ủy nhiệm để chấp nhận sự kiện chạm là một phần của bất kỳ lớp nào được thừa kế từ UIResponder chẳng hạn như UIView. Sử dụng phương thức ủy nhiệm phù hợp nhất với bạn. Nếu bạn muốn nó đi theo ngón tay của bạn, đây sẽ là -touchesMoved:

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

khác phương pháp đại biểu sẵn cho bạn là:

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

tôi đã viết một ứng dụng ví dụ mà thực hiện chính xác những gì bạn muốn. Đó là một bản demo của bản vẽ đồ họa Quartz 2D, nhưng nó vẽ một hình vuông màu đỏ và vòng tròn màu đen, nơi bạn kéo ngón tay của bạn và nên đủ đơn giản để làm theo:

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

Download Xcode project (32kb)

+1

Liên kết là xuống, bạn có thể cập nhật nó xin vui lòng? Tôi có chính xác cùng một vấn đề! – Seb

1

có bài đăng nổi bật tại đây.

bởi Divan Visagie

ở đây là mã có liên quan (lấy từ các diễn đàn):

- (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)]; 
        } 
    ]; 
} 
Các vấn đề liên quan