我想做一个应用程序,我需要一个工具栏。工具栏将具有不同数量的对象(图像)。这些对象应该是可拖动的,并放在视图的正上方。
这是一种绘图应用程序,用户可以在画布上选择任何形状和位置。谁能告诉我实现这一目标的最好方法?
提前谢谢。
发布于 2011-12-01 09:24:45
很难回答。
我要做的是使工具栏上的每一项都成为UIView,并覆盖touchesBegan、touchesEnded和touchesMoved。使您的画布成为另一个UIView。当用户单击一个项目进行拖动时,在某个地方注册该项目,比如一个正方形。然后,当它们落在画布上时,在该位置绘制该正方形。为了让它看起来更漂亮,可能会制作一个alpha为.4的UIImageView,让它随着用户的触摸移动,这样他们就知道自己拖的是什么,以及它会落在哪里。
下面是代码。UIViewController.h
@class ToolBox;
@interface ViewController : UIViewController {
IBOutlet UIView *canvas;
IBOutlet ToolBox *toolBox;
}
@property (nonatomic, retain) UIView *canvas;
@property (nonatomic, retain) ToolBox *toolBox;
@endUIViewController.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];
}
@endToolBox.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;
@endToolBox.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];
}
@endTool.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;
@endTool.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我的程序是这样出来的-
初始屏幕:工具已准备好拖放到画布上

将工具放在画布上

这是过渡的幽灵

发布于 2011-12-01 23:07:02
我不确定这将如何在objective c中实现,但我已经几乎完全按照您在java中所描述的那样完成了。基本上,当您开始拖动工具栏中的对象时,它将识别操作并将项目的新实例存储在我所称的传输容器中,这基本上是一个全局静态变量。这样,当拖动操作发生竞争(即丢弃)时,您只需从容器中抓取该对象并将其添加到它被丢弃的位置即可。
https://stackoverflow.com/questions/8332668
复制相似问题