我想创建一个应用程序,我需要一个工具栏。工具栏将具有各种数量的对象(图像)。这些对象应该是可拖动的,并放置在它正上方的视图中。
这是一种绘图应用程序,用户可以在画布上选择任何形状和位置。谁能告诉我最好的方法呢?
提前致谢。
答案 0 :(得分:6)
艰难的一个。
我要做的是让工具栏上的每个项目都是UIView并覆盖touchesBegan,touchesEnded和touchesMoved。让你的画布另一个UIView。当用户单击要拖动的项目时,请在某处注册该项目,例如正方形。然后当他们放在画布上时,在那个位置画出那个方块。为了使它看起来漂亮,可能会使用类似.4的alpha的UIImageView随着用户的触摸移动,这样他们就知道它们在拖动什么,以及它将落在哪里。
这是代码。 UIViewController.h
@class ToolBox;
@interface ViewController : UIViewController {
IBOutlet UIView *canvas;
IBOutlet ToolBox *toolBox;
}
@property (nonatomic, retain) UIView *canvas;
@property (nonatomic, retain) ToolBox *toolBox;
@end
UIViewController.m
#import "ViewController.h"
#import "ToolBox.h"
#import "Tool.h"
@implementation ViewController
@synthesize canvas, toolBox;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[self toolBox] setCanvas:[self canvas]];
Tool *tool = [[[Tool alloc] initWithFrame:CGRectMake(0,0,64,64)] autorelease];
[tool setImageView:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]] autorelease]];
[tool setImage:[UIImage imageNamed:@"1.png"]];
[tool addSubview:[tool imageView]];
[tool setToolBox:[self toolBox]];
[[self toolBox] addObject:tool];
}
@end
ToolBox.h
@class Tool;
@interface ToolBox : UIView {
NSMutableArray *tools;
UIView *canvas;
UIImage *currentTool;
}
@property (nonatomic, retain) UIImage *currentTool;
@property (nonatomic, retain) NSMutableArray *tools;
@property (nonatomic, retain) UIView *canvas;
-(void)addObject:(Tool *)newTool;
-(void)updatePositions;
@end
ToolBox.m
@implementation ToolBox
@synthesize tools, canvas, currentTool;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
-(void) dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
-(NSMutableArray *)tools {
if(tools == nil) {
[self setTools:[NSMutableArray array]];
}
return tools;
}
-(void)addObject:(Tool *)newTool {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(draggingTool:) name:@"Dragging New Tool" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(draggedTool:) name:@"Dragged Tool To" object:nil];
[[self tools] addObject:newTool];
[self updatePositions];
}
-(void)updatePositions {
int x = 0;
int y = 0;
int width = 64;
int height = 64;
for(Tool *button in [self tools]) {
[button setFrame:CGRectMake(x, y, width, height)];
x += width;
[self addSubview:button];
}
}
-(void)draggingTool:(NSNotification *)notif {
NSDictionary *dict = [notif userInfo];
UIImage *image = [dict valueForKey:@"Image"];
[self setCurrentTool:image];
}
-(void)draggedTool:(NSNotification *)notif {
UITouch *touch = [[notif userInfo] valueForKey:@"Touch"];
CGPoint point = [touch locationInView:canvas];
UIImageView *imageView = [[[UIImageView alloc] initWithImage:currentTool] autorelease];
[imageView setCenter:point];
[canvas addSubview:imageView];
}
@end
Tool.h
@class ToolBox;
@interface Tool : UIView {
UIImageView *imageView;
UIImage *image;
UIImageView *ghostImageView;
ToolBox *toolBox;
}
@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic, retain) UIImage *image;
@property (nonatomic, retain) UIImageView *ghostImageView;
@property (nonatomic, retain) ToolBox *toolBox;
@end
Tool.m
#import "Tool.h"
#import "ToolBox.h"
@implementation Tool
@synthesize imageView, image, ghostImageView, toolBox;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"tool touches began");
NSDictionary *dict = [NSDictionary dictionaryWithObject:[self image] forKey:@"Image"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"Dragging New Tool" object:nil userInfo:dict];
UITouch *touch = [touches anyObject];
CGPoint center = [touch locationInView:[[self toolBox] canvas]];
[self setGhostImageView:[[[UIImageView alloc] initWithImage:[self image]] autorelease]];
[[self ghostImageView] setCenter:center];
[[[self toolBox] canvas] addSubview:[self ghostImageView]];
[[self ghostImageView] setAlpha:.4];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"tool touches ended");
NSDictionary *dict = [NSDictionary dictionaryWithObject:[touches anyObject] forKey:@"Touch"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"Dragged Tool To" object:nil userInfo:dict];
[self setGhostImageView:nil];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"tool touch moved");
if([self ghostImageView] != nil) {
UITouch *touch = [touches anyObject];
CGPoint center = [touch locationInView:[[self toolBox] canvas]];
NSLog(@"Ghost : %2f, %2f", center.x, center.y);
[[self ghostImageView] setCenter:center];
}
}
@end
我的计划就是这样 -
初始屏幕:工具已准备好拖放到画布上
将工具放在画布上
这是转型的幽灵
答案 1 :(得分:0)
我不确定如何在目标c中实现它,但我几乎完全按照你在java中描述的内容完成了。基本上,当您开始在工具栏中拖动对象时,它将识别该操作并将该项的新实例存储在我称为传输容器的内容中,该传输容器基本上是一个全局静态变量。这样当拖动动作竞争时(即掉落),您可以从容器中抓取该对象并将其添加到丢弃的任何位置。