0

プログラムで複数の画像を作成し、それらのいずれかをビュー内で移動できるようにしようとしています。ボタンをクリックすると、その画像を移動できます。ボタンをもう一度クリックすると、その画像は移動できますが、最初に作成された画像は移動できなくなります。タグを使ってみました。

ヘッダ。

UIImageView *imageView;    
NSUInteger i;

@property (nonatomic, retain) UIImageView *imageView;

実装

@synthesize imageView;

-(IBAction)printTheImage:(id)sender{
    UIImageView *theImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"animage.png"]];
    self.imageView = theImage;
    self.imageView.userInteractionEnabled = YES;
    self.imageView.frame = CGRectMake(500, 500, 200, 200);
    imageView.tag = i;
    [self.view addSubview:self.imageView];
    i++;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches]anyObject];
    CGPoint location = [touch locationInView:self.view];
    if ([touch view]==self.imageView) 
        {
        if (ImageView.tag == 1){           
           self.imageView.center = location;
        }
        if (imageView.tag == 2){
           self.imageView.center = location;
        }
    }
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 
    [self touchesBegan:touches withEvent:event];
}

- (void)viewDidLoad{
    i = 1;
    [super viewDidLoad];
}
4

3 に答える 3

1

これが私があなたのコードから思いついたものです

//header
NSUInteger imageCount;
@property (nonatomic, retain) UIImageView *selectedImageView;

//implementation

@synthesize selectedImageView;

-(IBAction)printTheImage:(id)sender {
    UIImageView *imageView = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"animage"]];
    [imageView sizeToFit];
    imageView.frame = CGRectMake(500, 500, 200, 200);
    imageView.userInteractionEnabled = YES;
    imageView.tag = imageCount;
    [self.view addSubview: imageView];
    [imageView release];

    imageCount++;
}

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

    //iterate trough all images
    for (int i = 0; i < imageCount; i++) {
        //get image from tag
        UIImageView *imageView = (UIImageView *)[self.view viewWithTag: i];
        //check which image was tapped
        if (CGRectContainsPoint(imageView.frame, [touch locationInView:self.view])) {
            NSLog(@"Image #%i was tapped",i);
            self.selectedImageView = imageView;
            //don't waste processor time for checking all other images, get the first and break the loop
            break;
    }
}

これが画像を動かすものです:

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

    //move the tapped image
    self.selectedImageView.center = [touch locationInView:self.view];
}

イメージ カウンターをリセットし、selectedImageView を nil に設定することを忘れないでください。

于 2013-01-28T04:47:55.600 に答える
0

代わりにこれを試してください。

@property (nonatomic, copy) NSMutableArray *imageViews;


@synthesize imageView;

-(IBAction)printTheImage:(id)sender{
    UIImageView *theImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"animage.png"]];

    [self.imageViews adObject:theImage];

    theImage.userInteractionEnabled = YES;
    theImage.frame = CGRectMake(500, 500, 200, 200);
    imageView.tag = [imageViews indexOfObject:theImage];
    [self.view addSubview:theImage];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches]anyObject];
    CGPoint location = [touch locationInView:self.view];
    NSInteger viewIndex = [self.imageViews indexOfObject:[touch view]];
    if (viewIndex != NSNotFound) 

        UIView *theImage = self.imageViews[viewIndex];
        theImage.center = location;

    }
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 
    [self touchesBegan:touches withEvent:event];
}
于 2013-01-28T05:03:22.840 に答える