我是iOS开发的初学者,我正在为我的学校做一个副项目。我想做的是有一个小图像,用户可以从中拖出更多图像。从本质上讲,我有一个生成正方形的应用程序,我需要有一个设置的正方形,用户可以在其中触摸正方形并将新正方形拖到板上。我已经在一定程度上实现了这一目标,但还不是所需的行为。目前,用户必须触摸按钮,然后松开,然后才能与新方块交互以将其拖离按钮。我想要的是以某种方式将触摸事件传递给创建的新 UIImageView 对象,以便无缝拖出一个新方块。下面是我当前的代码。
//UIViewController
#import <UIKit/UIKit.h>
#import "Tile.h"
@interface MenuViewController : UIViewController
{
Tile *tileObject;
}
@end
#import "MenuViewController.h"
@implementation MenuViewController
- (IBAction)createTile:(UIButton *)sender {
CGFloat tileX = sender.center.x;
CGFloat tileY = sender.center.y;
tileObject = [[Tile alloc]initWithX:tileX andY:tileY];
[self.view addSubview:tileObject];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
@end
//Custom Tile Class
#import <UIKit/UIKit.h>
@interface Tile : UIImageView
{
CGPoint startLocation;
}
- (id)init; //Default Initialization
- (id)initWithX:(CGFloat)x andY:(CGFloat)y; //Initialization with coordinate points from single screen tap
@end
#import "Tile.h"
@implementation Tile
//Default initialization
-(id) init
{
self = [self initWithX:0 andY:0];
return self;
}
//Initialization with coordinate points from single screen tap
- (id)initWithX:(CGFloat)centerX andY:(CGFloat)centerY
{
//Creates a UIImageView object from an image file
UIImage *image = [UIImage imageNamed:@"redblock.png"];
self = [super initWithImage:image];
//Used to center the image under the single screen tap
centerX -= (image.size.height/2);
centerY -= (image.size.height/2);
//Sets the position of the image
self.frame = CGRectMake(centerX, centerY, image.size.width, image.size.height);
self.userInteractionEnabled = YES; //required for interacting with UIImageViews
return self;
}
/* Methods from Stack Overflow
http://stackoverflow.com/questions/1991899/uiimage-detecting-touch-and-dragging
Referenced from http://www.iphoneexamples.com/
*/
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
// Retrieve the touch point
CGPoint point = [[touches anyObject] locationInView:self];
startLocation = point;
[[self superview] bringSubviewToFront:self];
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
// Move relative to the original touch point
CGPoint point = [[touches anyObject] locationInView:self];
CGRect frame = [self frame];
frame.origin.x += point.x - startLocation.x;
frame.origin.y += point.y - startLocation.y;
[self setFrame:frame];
}
/*
end of methods from Stack Overflow
*/
@end
如果初始触摸点可以是图像视图,其中要拖动的图像作为其图像,那么您可以这样做:
在你的menuViewController类中,把它放在viewDidLoad中(并删除按钮及其方法):
- (void)viewDidLoad
{
[super viewDidLoad];
tileObject = [[Tile alloc]initWithX:160 andY:200];
[self.view addSubview:tileObject];
}
然后在 Tile 类中,在 touchesBestarted:withEvent: 方法中创建一个新磁贴,并像以前一样执行拖动:
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
Tile *newTile = [[Tile alloc]initWithX:160 andY:200];
[self.superview addSubview:newTile];
// Retrieve the touch point
CGPoint point = [[touches anyObject] locationInView:self];
startLocation = point;
[[self superview] bringSubviewToFront:self];
}
您需要执行以下操作,而不是使用带有 touchDown 事件的按钮:
1) 检测触摸开始并创建您的磁贴
2) 检测触摸移动并移动您的磁贴