0

私は長い間ネットを検索してきましたが、画像ビューをドラッグ可能にする具体的な方法を見つけられませんでした。これが私がこれまでに持っているものです:

tempViewController.h

#import <UIKit/UIKit.h>
#import "MyRect.h"
@class UIView;
@interface tempViewController : UIViewController

@property (nonatomic, strong) MyRect *rect1;
@end

tempViewController.m

#import "tempViewController.h"

@interface tempViewController ()

@end

@implementation tempViewController

@synthesize rect1 = _rect1;

- (void)viewDidLoad
{
    [super viewDidLoad];

    _rect1 = [[MyRect alloc]initWithFrame:CGRectMake(150.0, 100.0, 80, 80)];
    [_rect1 setImage:[UIImage imageNamed:@"cloud1.png"]];
    [_rect1 setUserInteractionEnabled:YES];
    [self.view addSubview:_rect1];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches]anyObject];
    if([touch view] == _rect1)
    {
        CGPoint pt = [[touches anyObject] locationInView:_rect1];
        NSLog(@"%@",NSStringFromCGPoint(pt));
        _rect1.center = pt;
    }
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches]anyObject];
    if([touch view] == _rect1)
    {
        CGPoint pt = [[touches anyObject] locationInView:_rect1];
        NSLog(@"%@",NSStringFromCGPoint(pt));
        _rect1.center = pt;
    }
}


@end

MyRect現在、空のUIImageViewクラスです。

マイクロンなどのポイントから画像をドラッグすると、画像[532,589]は画面のまったく別の部分に移動します。[144, 139]

4

2 に答える 2

3

ビューにを添付するだけUIPanGestureRecognizerです。レコグナイザーのアクションで、レコグナイザーの「変換」(オフセット)に基づいてビューの中心を更新してから、レコグナイザーの変換をゼロにリセットします。次に例を示します。

- (void)viewDidLoad {
    [super viewDidLoad];

    UIView *draggableView = [[UIView alloc] initWithFrame:CGRectMake(150, 100, 80, 80)];
    draggableView.userInteractionEnabled = YES;
    draggableView.backgroundColor = [UIColor redColor];
    [self.view addSubview:draggableView];

    UIPanGestureRecognizer *panner = [[UIPanGestureRecognizer alloc]
        initWithTarget:self action:@selector(panWasRecognized:)];
    [draggableView addGestureRecognizer:panner];
}

- (void)panWasRecognized:(UIPanGestureRecognizer *)panner {
    UIView *draggedView = panner.view;
    CGPoint offset = [panner translationInView:draggedView.superview];
    CGPoint center = draggedView.center;
    draggedView.center = CGPointMake(center.x + offset.x, center.y + offset.y);

    // Reset translation to zero so on the next `panWasRecognized:` message, the
    // translation will just be the additional movement of the touch since now.
    [panner setTranslation:CGPointZero inView:draggedView.superview];
}
于 2013-01-03T23:49:14.037 に答える
0

-touchesBegan:withEvent:などは使用しないでください。

そのような「高レベル」のものに使用したいのはUIGestureRecognizersです。

パンジェスチャレコグナイザーをビューセットデリゲート(場合によってはビュー自体)に追加し、コールバックでレコグナイザーが移動した距離だけビューを移動します。

初期位置を覚えて-translationInView毎回移動するか、単に平行移動で移動して-setTranslation:inView:から、レコグナイザーの平行移動をゼロにリセットするために使用するため、デリゲートメソッドの次の呼び出しで、最後の呼び出し以降の移動が再び取得されます。 。

于 2013-01-03T23:46:07.293 に答える