2013-02-19 33 views
0

我正在開發中,我創建了一個UIImageView項目顯示的UIImageViews後不工作:touchmoved動態使用<code>NSMutableArray</code></p> <p>的代碼,這是從NSMutable陣列

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,我在做這樣的代碼:它打印的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方法被調用,之後。

我在這裏遇到困難,也經歷了許多問題/答案,但找不到工作解決方案。任何幫助將不勝感激。感謝好回覆

+0

聲明myArray強,並在初始化時,使用self.myArray = [NSMutableArray數組]; – samfisher 2013-02-19 13:31:45

+0

stacktrace會有幫助。 – Shashank 2013-02-19 13:36:20

+0

不清楚你在touchesbegan函數中做什麼? – 2013-02-19 13:37:15

回答

0

爲了使您的imageviews拖動,

1)全部刪除 - (無效)剛剛創建的UIImageView * tempImageView(環路)

後觸及方法

2)添加這兩條線

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];} 
+0

我使用touchesBegan,touchesMoved,touchesEnded方法>>和任何使用該方法的建議? – 2013-02-19 13:59:20

+0

我的建議是不要使用touchesBegan,touchesMoved,touchesEnded在您的代碼拖動imageviews。使用GestureRecognizers使您的代碼非常清晰,並且這是推薦的方式。 – 2013-02-19 14:05:24

+0

然後如何檢索觸摸座標>>用戶觸摸uiimageview的位置>> – 2013-02-20 05:40:06

相關問題