2011-12-28 19 views
8

Trong App iPhone của tôi, tôi muốn chức năng thực hiện như hình dưới đâyiPhone App: thực hiện bằng cách kéo và thả hình ảnh trong UIView

enter image description here

người dùng có thể chọn một hình dạng (hình ảnh) từ một quan điểm và đưa vào view khác

Làm thế nào tôi có thể đạt được điều này?

hãy giúp đỡ và đề nghị.

Cảm ơn.

+0

[http://stackoverflow.com/questions/17678735/adding-drag-and-drop-component-to-ios-app/31963306#31963306](http://stackoverflow.com/questions/17678735/adding-drag-and-drop- thành phần-to-ios-app/31963306 # 31963306) Bạn có thể thử liên kết này tận hưởng mã hóa :) – SARATH

Trả lời

4

Hãy thử bên dưới liên kết drag and drop image around the screen

cũng try this

+1

hi, lin đầu tiên của bạn k đã chết. để tham khảo trong tương lai, bạn có thể đăng nội dung của liên kết trong nhận xét của mình không. SO có nghĩa là một cửa hàng một cửa và không nên liên kết đến các liên kết có khả năng chết – lizzy81

-2

Ba giải pháp bước

1) u'll phải thực hiện/nghe cho cảm ứng sự kiện

2) thực hiện kéo và cảm ứng sự kiện

3) và sau đó quản lý phương pháp movetosuperview.

+0

Mã nguồn để biết cách kéo và thả bằng snap đến khu vực http://stackoverflow.com/a/9233102/1537178 –

5

Cách dễ nhất là sử dụng một UIPanGestureRecognizer. Điều đó sẽ cung cấp cho bạn thông báo khi chế độ xem được di chuyển và khi nó bị xóa. Khi nó được di chuyển, hãy cập nhật trung tâm của nó. Khi nó được bỏ, kiểm tra xem vị trí của nó nằm trong giới hạn của khung nhìn mà bạn muốn thả xuống hay không. (Bạn có thể cần chuyển đổi tọa độ sang hệ tọa độ của khung nhìn đích.) Nếu có, hãy thực hiện hành động thích hợp.

1

Bạn có thể tham khảo các bài trước đó chắc chắn sẽ giúp bạn đạt được chức năng này ...

  1. Basic Drag and Drop in iOS
  2. Building drag and drop interface on iphone
  3. iPhone drag/drop

Đối với tất cả các liên kết trên, Bạn phải ghi nhớ rằng Trước tiên bạn cần nhận được sự kiện TouchesBegin cho bất kỳ Kiểm soát nào và sau đó bạn phải nhận được TouchesMoved sự kiện để kiểm soát tương tự.

Trong TouchesMoved sự kiện, bạn chỉ cần có để có được điểm trung tâm (CGPoint) của Control. Và khi bạn thả điều khiển sẽ được đặt tại số CGPoint đó. Nếu điều này tạo ra sự cố thì bạn có thể thực hiện biến số CGPoint đó và đặt điểm đó trong sự kiện TouchesEnded.

Đối với trường hợp của bạn, tôi nghĩ rằng bạn phải có để duy trì Hierarchy của Views ... Else trong khi kéo bạn xem có thể không nhìn thấy được ...

để mã hóa PHẦN THÊM:

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

     NSLog(@"%f,%f", self.center.x, self.center.y); 
     CGPoint newLoc = CGPointZero; 

     newLoc = [self.mainView convertPoint:[[touches anyObject] locationInView:self.superview] toView:self.superview]; 
     float newX = newLoc.x + self.superview.frame.origin.x + (self.frame.size.width /2) + [[touches anyObject] locationInView:self].x ; 
     float newY = newLoc.y - (((UIScrollView *)self.superview).contentOffset.y *2) ; 
     NSLog(@"content offset %f", ((UIScrollView *)self.superview).contentOffset.y); 

     self.scrollParent.scrollEnabled = NO; 
     NSLog(@"%f,%f", self.center.x, self.center.y); 

     newLoc = CGPointMake(newX, newY); 
     [self.superview touchesCancelled:touches withEvent:event];                
     [self removeFromSuperview]; 
     NSLog(@"%f,%f", self.center.x, self.center.y); 


     self.center = CGPointMake(newLoc.x, newLoc.y); 
     [self.mainView addSubview:self]; 
     NSLog(@"%f,%f", self.center.x, self.center.y); 

     [self.mainView bringSubviewToFront:self]; 
     isInScrollview = NO; 
} 

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

    [UIView beginAnimations:@"stalk" context:nil]; 
    [UIView setAnimationDuration:.001]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 

    UITouch *touch = [touches anyObject]; 

    self.center = [touch locationInView: self.superview]; 

    [UIView commitAnimations]; 
    if ((self.center.x + (self.frame.size.width/2)) > 150 && hasExitedDrawer && !self.scrollParent.dragging) { 
     self.scrollParent.scrollEnabled = NO; 

     [self.delegate moveItemsDownFromIndex: ((self.center.y + (self.scrollParent.contentOffset.y))/44) + 1 ]; 
     //NSLog(@"%i", ((self.center.y + (self.scrollParent.contentOffset.y *2))/44) + 1); 
    } 

    if (self.center.x + (self.frame.size.width/2) < 150) { 

     hasExitedDrawer = YES; 

    } 

} 

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

    if ((self.center.x + (self.frame.size.width/2)) > 150 && hasExitedDrawer && !self.scrollParent.dragging) { 
     CGPoint newLoc = CGPointZero; 

     newLoc = [self.scrollParent convertPoint:[[touches anyObject] locationInView:self.superview] toView:self.superview]; 
     float newY = newLoc.y + (self.scrollParent.contentOffset.y *2); 

     [self.scrollParent insertSubview:self atIndex:((self.center.y + (self.scrollParent.contentOffset.y))/44) ]; 
     self.frame = CGRectMake(0, newY, self.frame.size.width, self.frame.size.height); 
     isInScrollview = YES; 
     hasExitedDrawer = NO; 
    } 
} 

mã này có thể cantains một số không liên quan nhưng mang đến cho bạn ý tưởng thêm ...

Các vấn đề liên quan