English 中文(简体)
从固定的 UIImaageView 或按钮中拖出新的 UIImaageView 对象
原标题:Drag out new UIImageView objects from a stationary UIImageView or button

iOS 开发时我是一个初学者, 我正为学校做一个侧边工程。 我想做的是有一个小图像, 用户可以从中拖动更多的图像。 基本上我有一个程序, 在那里我正在生成方形, 我需要有一套方形, 用户可以触摸方形, 并将新的方形拖到一个棋盘上。 我达到了这个目的, 但这不是想要的行为 。 目前, 用户必须触动按钮, 然后在他们与新方形互动之前释放出来, 才能将它拖下按钮 。 我想要的是让触摸事件以某种方式传递到新建的 UIImagiView 对象, 这样它就可以无缝地拖动一个新的方形 。 下面是我当前使用的代码 。

//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
最佳回答

如果初始触地点可以是一个图像视图, 与您想要拖动的图像作为图像, 那么您可以这样做 :

在您的菜单 View Connator 类中, 请将此放入视图中 。 DidLoad (并删除按钮及其方法) :

- (void)viewDidLoad
{
    [super viewDidLoad];
    tileObject = [[Tile alloc]initWithX:160 andY:200];
    [self.view addSubview:tileObject];
}

然后在你的瓷砖类中, 在触摸的Began:与Event:方法 你创造了一个新的瓷砖, 并像以前一样执行拖拉:

- (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];
}
问题回答

不需要使用触碰下的事件按钮, 您需要 :

(1) 检测触摸Began 并创建您的瓷砖

(2) 检测触摸 移动和移动您的瓷砖





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签