Я долго искал в сети и не нашел конкретного способа сделать изображение перетаскиваемым. Вот что у меня есть до сих пор:
tempViewController.h
#import <UIKit/UIKit.h>
#import "MyRect.h"
@class UIView;
@interface tempViewController : UIViewController
@property (nonatomic, strong) MyRect *rect1;
@end
tempViewController.m
#import "tempViewController.h"
@interface tempViewController ()
@end
@implementation tempViewController
@synthesize rect1 = _rect1;
- (void)viewDidLoad
{
[super viewDidLoad];
_rect1 = [[MyRect alloc]initWithFrame:CGRectMake(150.0, 100.0, 80, 80)];
[_rect1 setImage:[UIImage imageNamed:@"cloud1.png"]];
[_rect1 setUserInteractionEnabled:YES];
[self.view addSubview:_rect1];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches]anyObject];
if([touch view] == _rect1)
{
CGPoint pt = [[touches anyObject] locationInView:_rect1];
NSLog(@"%@",NSStringFromCGPoint(pt));
_rect1.center = pt;
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches]anyObject];
if([touch view] == _rect1)
{
CGPoint pt = [[touches anyObject] locationInView:_rect1];
NSLog(@"%@",NSStringFromCGPoint(pt));
_rect1.center = pt;
}
}
@end
MyRect
прямо сейчас является пустым классом UIImageView.
Перетаскивание изображения из точки, например [532,589]
на микрон, перемещает его в совершенно другую часть экрана, например [144, 139]
.