2

私は 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://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
4

2 に答える 2

2

touchDown イベントでボタンを使用する代わりに、次のことを行う必要があります。

1) touchesBegan を検出してタイルを作成する

2) touchesMoved を検出してタイルを移動する

于 2012-05-24T15:56:53.160 に答える
2

最初のタッチ ダウン ポイントが、ドラッグしたい画像を画像として含む画像ビューである場合、次のように行うことができます。

menuViewController クラスで、これを viewDidLoad に入れます (そしてボタンとそのメソッドを削除します):

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

次に、Tile クラスの touchesBegan:withEvent: メソッドで新しいタイルを作成し、以前と同じようにドラッグを実行します。

- (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];
}
于 2012-05-24T20:42:50.423 に答える