1

次のコードを書きましたが、動作しません。画像は表示されますが移動できません

UIImageView *oneselement = [[UIImageView alloc] initWithFrame:self.view.frame];
oneselement = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1el.png"]];
oneselement.frame = CGRectMake(0, 0, 122, 122);
[self.view addSubview:oneselement];


-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];

    if([touch view]==oneselement)
    {
        CGPoint location = [touch locationInView:self.view];
        oneselement.center=location;
    }}

表示する画像を追加して、次のコードを記述しました

.h

@interface ViewController : UIViewController
{
    IBOutlet UIImageView *oneEl;
}
@property (nonatomic,retain) IBOutlet UIImageView *oneEl;

.m

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
    if([touch view]==oneEl)
    {
        CGPoint location = [touch locationInView:self.view];
        oneEl.center=location;
    }
}

そしてそれは動作します!uiimageviewによってプログラムで作成されたものが移動できるようにするにはどうすればよいですか?

NSMUTABLEARRAY

self.elements=[myElements getElements];

imagesElements = [[NSMutableArray alloc]init];
for(ElemetsList *item in self.elements)
{
        UIImageView *oneelement = [[UIImageView alloc] initWithImage:[UIImage imageNamed:item.imgElement]];
        oneelement.frame = CGRectMake(item.positionX, item.positionY, item.width, item.height);
        oneelement.userInteractionEnabled=YES;
        [imagesElements addObject:oneelement];
}

for(UIImageView *img in imagesElements) 
    [self.view addSubview:img];

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];

    for(UIImageView *img in imagesElements) 
    {  
        if([self view] == img)
        {
            CGPoint location = [touch locationInView:self.view];
            img.center=location;
        }
    }
}
4

2 に答える 2

3

次のコードのようにUIImageViewを初期化すると、2番目の例のように機能します。

ViewController.h

@interface ViewController : UIViewController
{
    UIImageView *oneselement;
}
@property (nonatomic,retain) UIImageView *oneselement;

ViewController.m

//initialize your UIImageView
UIImageView *newOneselement = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1el.png"]];
self.oneselement = newOneselement;
self.oneselement.userInteractionEnabled = YES;
self.oneselement.frame = CGRectMake(0, 0, 122, 122);
[self.view addSubview:self.oneselement];
[newOneselement release];

//Handle touchesMoved
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    if([touch view]==self.oneselement)
    {
        CGPoint location = [touch locationInView:self.view];
        self.oneselement.center=location;
    }
}
于 2012-07-18T12:19:49.377 に答える
0

このリンクをご覧ください。
また、試すことができます:

画像を移動するには、移動するコードをアニメーションブロックで囲む必要があります。

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
tapImage.center = CGPointMake(156, 110);
[UIView commitAnimations];

UIView setAnimationDidStopSelector:メソッドを使用して、アニメーションの完了時に実行するメソッドを作成することもできます。

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animateImages)];
tapImage.center = CGPointMake(156, 110);
[UIView commitAnimations];

//other stuff

-(void)animateImages{
     [tapImage startAnimating];
}
于 2012-07-18T12:25:39.303 に答える