从固定的 UIImageView 或按钮中拖出新的 UIImageView 对象
Posted
技术标签:
【中文标题】从固定的 UIImageView 或按钮中拖出新的 UIImageView 对象【英文标题】:Drag out new UIImageView objects from a stationary UIImageView or button 【发布时间】:2012-05-24 15:11:16 【问题描述】:我是 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://***.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
【问题讨论】:
您希望按钮(或最终用于初始触摸的任何按钮)看起来与您拖动的图像相同,还是需要不同? 【参考方案1】:如果初始触地点可以是一个图像视图,其中您要拖动的图像作为其图像,那么您可以这样做:
在您的 menuViewController 类中,将其放入 viewDidLoad(并删除按钮及其方法):
- (void)viewDidLoad
[super viewDidLoad];
tileObject = [[Tile alloc]initWithX:160 andY:200];
[self.view addSubview:tileObject];
然后在你的 Tile 类中,在 touchesBegan:withEvent: 方法中创建一个新的 tile,并像以前一样执行拖动操作:
- (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];
【讨论】:
【参考方案2】:不要使用带有 touchDown 事件的按钮,您需要:
1) 检测 touchesBegan 并创建您的磁贴
2) 检测 touchesMoved 并移动您的磁贴
【讨论】:
对不起,我有点困惑。您是说按钮应该只是另一个处理 touchesBegan 和 touchesMoved 的 UIImageView。我确实有一个处理这些触摸的 UIImageView 子类的 Tile 类。如何让原始按钮或 UIImageView 也处理这些事件,然后将触摸中间事件传递给新创建的具有相同触摸事件的 UIImageView? 新创建的磁贴不需要处理任何触摸事件,直到您想稍后移动它。因此,您不需要将触摸传播到新图块。您只需使用以原始按钮/图像触摸开头的按钮即可将其首次拖出图块。以上是关于从固定的 UIImageView 或按钮中拖出新的 UIImageView 对象的主要内容,如果未能解决你的问题,请参考以下文章
在 Selenium WebDriver 中拖放的 JavaScript 解决方法
java swing 编程中,如何实现点击按钮弹出新的窗口???