黒いボタンが三角形の外に移動しないようにし(Flickrのリンクを参照)、三角形の端を超えると、ボタンが三角形に戻されるようにします。
それ、どうやったら出来るの?
http://www.flickr.com/photos/92202376@N08/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