我已经在网上搜索了很长时间,但我还没有找到一种使图像视图可拖动的具体方法。这是我到目前为止所拥有的:
临时视图控制器.h
#import <UIKit/UIKit.h>
#import "MyRect.h"
@class UIView;
@interface tempViewController : UIViewController
@property (nonatomic, strong) MyRect *rect1;
@end
临时视图控制器.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]