0

我正在开发一个项目,在该项目中我UIImageView使用NSMutableArray

代码是:

for (int i = 0; i < index ; i++) {
    image_name= [NSString stringWithFormat:@"%@%dpng.png",[string_values objectAtIndex:0],i+1];

    UIImageView *tempImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:image_name]];

    tempImageView.userInteractionEnabled = YES;
    CGSize size2=tempImageView.image.size;
    int width2=size2.width/2;
    int height2 =size2.height/2;
    tempImageView.frame = CGRectMake(xvalue,yvalue,width2,height2);  

    [myArray addObject:tempImageView];
}

之后,我添加子视图以使用以下代码在屏幕上显示它:

for(int i=0;i<[myArray count];i++) {
    [self.view addSubview:[myArray objectAtIndex:i]];
}

以上所有代码都可以根据需要运行良好。现在对于 touchesBegan 方法,我正在执行以下代码:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    printf("the touch is fired");
    for(int i=0;i<[myArray count];i++) {
        [self.view addSubview:[myArray objectAtIndex:i]];
        touchStart = [[touches anyObject] locationInView:[myArray objectAtIndex:i]];
    }
}

调用该touchesBegan方法时,应用程序在打印 touchesBegan 方法的第一行之后立即崩溃。

我在这里遇到了困难,也经历了许多问题/答案,但找不到有效的解决方案。任何帮助将不胜感激。感谢您的好回复

4

1 回答 1

0

为了使您的图像视图可拖动,

1) 删除所有 - (void)touches 方法

2) 在创建 UIImageView *tempImageView (in loop) 之后添加这两行

 UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleDrag:)];
[tempImageView addGestureRecognizer:panRecognizer];

3)然后,将此方法添加到您的视图 controller.m 文件中的某处

-(void) handleDrag:(UIPanGestureRecognizer *) panGesture{
CGPoint transition = [panGesture translationInView:self.view];
panGesture.view.center = CGPointMake(panGesture.view.center.x + transition.x, panGesture.view.center.y +transition.y);
[panGesture setTranslation:CGPointMake(0,0) inView:self.view];}
于 2013-02-19T13:55:21.710 回答