Asp*_*per 0 iphone drawing drag-and-drop ipad
我想制作一个应用程序,我需要一个工具栏.工具栏将具有各种数量的对象(图像).这些对象应该是可拖动的,并放置在它正上方的视图上.
这是一种绘图应用程序,用户可以在画布上选择任何形状和位置.谁能告诉我最好的方法呢?
提前致谢.
艰难的一个.
我要做的是让工具栏上的每个项目都是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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
我的计划就像这样 -
初始屏幕:工具已准备好拖放到画布上

将工具放在画布上

这是转型的幽灵

| 归档时间: |
|
| 查看次数: |
1106 次 |
| 最近记录: |