我有一個問題,如何設置可拖動對象邊界的目標C
我想的黑色按鈕不遷出三角形的,(見Flickr的鏈接) ,如果越過的邊緣三角形,該按鈕返回到三角形。
我該怎麼做?
鏈接:http://www.flickr.com/photos/[email protected]/8590549046/in/photostream
#import "DraggableViewController.h"
@interface DraggableViewController()
@end
@implementation DraggableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// create a new button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:@"Scale9Image.png"]
forState:UIControlStateNormal];
// add drag listener
[button addTarget:self action:@selector(wasDragged:withEvent:)
forControlEvents:UIControlEventTouchDragInside];
// center and size
button.frame = CGRectMake((self.view.bounds.size.width - 100)/1.6,
(self.view.bounds.size.height - 50)/1.9,
40, 40);
button.frame.origin.y;
[self.view addSubview:button];
}
- (void)wasDragged:(UIButton *)button withEvent:(UIEvent *)event
{
// get the touch
UITouch *touch = [[event touchesForView:button] anyObject];
// get delta
CGPoint previousLocation = [touch previousLocationInView:button];
CGPoint location = [touch locationInView:button];
CGFloat delta_x = location.x - previousLocation.x;
CGFloat delta_y = location.y - previousLocation.y;
// move button
button.center = CGPointMake(button.center.x + delta_x,
button.center.y + delta_y);
}
@end
當您跟蹤拖動時,請勿將黑色按鈕移動到三角形之外。 (如果你有更具體的問題,我可以給你一個更具體的答案。) – Caleb 2013-03-25 19:41:33
@Caleb我該怎麼問?英語不是我的第一語言......我發現它很難解釋......對不起......但我的意思是:照片上的黑色按鈕不能出現在三角形中......所以按鈕必須留在那個你知道的某個「地區」? – matthijs 2013-03-25 19:43:13
你應該告訴我們你是如何管理拖動按鈕的。你是否在跟蹤自己?使用手勢識別器?做別的事嗎?也許顯示拖動跟蹤的代碼。無論如何,你顯然有一些代碼在移動按鈕,對吧?基本上,您需要修改該代碼,以便它不會將按鈕移動到您決定的任何邊界之外。就目前而言,我們無法討論該代碼,因爲只有我們其中一個人可以看到它。 – Caleb 2013-03-25 19:54:43