2013-05-15 153 views
2

我想刷我的圖像視圖,但沒有交換到左側。 我從frontcard陣列獲取圖像。我想在imageview中顯示並垂直滑動。滑動手勢不工作

我想這一個

FrontsCards =[[NSMutableArray alloc]initWithCapacity:9]; 
    [FrontsCards insertObject:@"cloub1.png" atIndex:0]; 
    [FrontsCards insertObject:@"cloub2.png" atIndex:1]; 
    [FrontsCards insertObject:@"cloub3.png" atIndex:2]; 
    [FrontsCards insertObject:@"cloub4.png" atIndex:3]; 
    [FrontsCards insertObject:@"cloub5.png" atIndex:4]; 
    [FrontsCards insertObject:@"cloub6.png" atIndex:5]; 
    [FrontsCards insertObject:@"cloub7.png" atIndex:6]; 
    [FrontsCards insertObject:@"cloub8.png" atIndex:7]; 
    [FrontsCards insertObject:@"cloub9.png" atIndex:8]; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 


    UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipeHandle:)]; 
    leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft; 
    [leftRecognizer setNumberOfTouchesRequired:1]; 
    [ImgView addGestureRecognizer:leftRecognizer]; 
} 

- (void)leftSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer 
{ 
    int m = 0; 

    NSLog(@"Left Swipe"); 

    AppDelegate * delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate]; 

    NSLog(@"%d",m); 


    for(m=0; m<[delegate.FrontsCards count];m++) 
    { 

     int randIdx=arc4random()%[delegate.FrontsCards count]; // Randomly  shufffled 
     ImgView.userInteractionEnabled=YES; 
     ImgView.image=[UIImage imageNamed:[delegate.FrontsCards objectAtIndex:randIdx]]; 

     NSLog(@"%d",m); 

    } 



} 

圖像不會被換到ImageView的。 如何解決我的問題幫我解決這個問題。 在此先感謝。

+0

控制檯是否顯示來自leftSwipeHandle:方法的日誌? – Hacker

+0

是否調用了'leftSwipeHandle'方法,是否檢查了中斷點 –

+0

UIImageView將默認禁用userInteraction,您需要將其明確設置爲YES。 – Amar

回答

2

嘗試使用這一個。因爲默認情況下圖像的用戶交互是NO。所以你需要設置用戶交互YES

FrontsCards =[[NSMutableArray alloc]initWithCapacity:9]; 
    [FrontsCards insertObject:@"cloub1.png" atIndex:0]; 
    [FrontsCards insertObject:@"cloub2.png" atIndex:1]; 
    [FrontsCards insertObject:@"cloub3.png" atIndex:2]; 
    [FrontsCards insertObject:@"cloub4.png" atIndex:3]; 
    [FrontsCards insertObject:@"cloub5.png" atIndex:4]; 
    [FrontsCards insertObject:@"cloub6.png" atIndex:5]; 
    [FrontsCards insertObject:@"cloub7.png" atIndex:6]; 
    [FrontsCards insertObject:@"cloub8.png" atIndex:7]; 
    [FrontsCards insertObject:@"cloub9.png" atIndex:8]; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 


    UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipeHandle:)]; 
    leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft; 
    [leftRecognizer setNumberOfTouchesRequired:1]; 
    [ImgView addGestureRecognizer:leftRecognizer]; 

    // --------------- Add this line here ------------ 
    ImgView.userInteractionEnabled = YES; 
} 

- (void)leftSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer 
{ 
    int m = 0; 

    NSLog(@"Left Swipe"); 

    AppDelegate * delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate]; 

    NSLog(@"%d",m); 


    for(m=0; m<[delegate.FrontsCards count];m++) 
    { 

     int randIdx=arc4random()%[delegate.FrontsCards count]; // Randomly  shufffled 
     ImgView.image=[UIImage imageNamed:[delegate.FrontsCards objectAtIndex:randIdx]]; 

     NSLog(@"%d",m); 

    } 
} 
+0

檢查我的代碼是否正確 – Jitendra

+0

ImgView.userInteractionEnabled = YES;在viewDidLoad中不寫入此行 - (void)leftSwipeHandle:(UISwipeGestureRecognizer *)gestureRecognizer。並看到我編輯的答案 –

+0

圖像左側交換時不顯示。 – Jitendra

0

您需要在UIImageView上設置imageview.userInteractionEnabled = YES;

+0

設置它,但仍然不顯示。我的代碼是正確的還是錯誤的 – Jitendra

+0

打印圖像名稱[delegate.FrontsCards objectAtIndex:randIdx]是正確還是空白? –

+0

仍然不適合我。 – Jitendra

1

在.h文件中

int imgCount; 

在.m文件試試這個

[ImgView addGestureRecognizer:leftRecognizer]; 
    //Add this 
    ImgView.userInteractionEnabled=YES; 
2

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    ImgView.userInteractionEnabled=YES; 
    UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipeHandle:)]; 
    leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft; 
    [leftRecognizer setNumberOfTouchesRequired:1]; 
    [ImgView addGestureRecognizer:leftRecognizer]; 

    imgCount = 0; 
} 

- (void)leftSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer { 
    AppDelegate * delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate]; 
    ImgView.image=[UIImage imageNamed:[delegate.FrontsCards objectAtIndex:imgCount]]; 
    imgCount++; 
    if (imgCount >= 9) { 
     imgCount = 0; 
    } 
} 
+0

答案的第一部分可能是有效的,但imgCount的使用是無效的,它消除了隨機性。 –

+0

對我來說工作不正常 – Jitendra

+0

是的,但沒有必要像在問題中提到的那樣在leftSwipeHandle方法中編寫循環。所以,我寫這個代碼...如果這是工作,然後Imodify一些代碼...這是爲了檢查目的,圖像是否變化... – DharaParekh

0

Tr的這個。

UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipeHandle:)]; 
     leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft; 
     [leftRecognizer setNumberOfTouchesRequired:1]; 
     [ImgView addGestureRecognizer:leftRecognizer]; 
    ImgView.userInteractionEnabled = YES; 

默認情況下UIImageView被禁用。您需要啓用它。