2013-08-06 63 views
0

我有MjpegView這是從mjpeg-iphone下載(以MJPEG-iphone的項目,它被命名爲MJPEGClient,我把它改名爲MjpegView更容易理解它的功能)MjpegView不響應UITapGestureRecognizer

基本上,MjpegView是擴展的UIImageView並使用NSUrlConnection從url獲取mjpeg圖像。我將UITapGestureRecognizer添加到MjpegView,但當我點擊時它沒有響應。 MjpegView的

代碼段如下:

@interface MjpegView : UIImageView 
@end 

@implementation MjpegView 

- (id)initWithFrame:(CGRect)frame { 
    self = [super initWithFrame:frame]; 

    if (self) { 

     if (_endMarkerData == nil) { 
      uint8_t endMarker[2] = END_MARKER_BYTES; 
      _endMarkerData = [[NSData alloc] initWithBytes:endMarker length:2]; 
     } 

     self.contentMode = UIViewContentModeScaleAspectFit; 

     singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onSingleTap:)]; 
     singleTapRecognizer.numberOfTapsRequired = 1; 
     singleTapRecognizer.numberOfTouchesRequired = 1; 

     [self addGestureRecognizer:singleTapRecognizer]; 

     [[NSURLConnection alloc] initWithRequest:[NSURLRequest requestWithURL:_url] delegate:self]; 
    } 

    return self; 
} 


#pragma mark - NSURLConnection Delegate Methods 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    //NSLog(@"didReceiveResponse"); 

    if (_receivedData) { 
     [_receivedData release]; 
    } 

    _receivedData = [[NSMutableData alloc] init]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    //NSLog(@"didReceiveData"); 

    [_receivedData appendData:data]; 

    NSRange endRange = [_receivedData rangeOfData:_endMarkerData 
              options:0 
              range:NSMakeRange(0, _receivedData.length)]; 

    long long endLocation = endRange.location + endRange.length; 
    if (_receivedData.length >= endLocation) { 
     NSData *imageData = [_receivedData subdataWithRange:NSMakeRange(0, endLocation)]; 
     UIImage *receivedImage = [UIImage imageWithData:imageData]; 
     if (receivedImage) { 
      self.image = receivedImage; 
     } 
    } 
} 

@end 

我懷疑是因爲主線程是忙更新與mpjeg圖像,從而無法檢測到抽頭視圖。任何人都有如何實施這個建議?

+0

你在哪裏添加UITapGestureRecognizer你的看法? – JeffRegan

+0

我在這裏錯過了它,已經編輯了我的問題。它在initWithFrame – Melissa

回答

1

默認情況下UIImageView已經userInteractionEnabled是假

設置屬性userInteractionEnabled= YES

試試這個,

@implementation MjpegView 

- (id)initWithFrame:(CGRect)frame { 
    self = [super initWithFrame:frame]; 

    if (self) { 

     if (_endMarkerData == nil) { 
      uint8_t endMarker[2] = END_MARKER_BYTES; 
      _endMarkerData = [[NSData alloc] initWithBytes:endMarker length:2]; 
     } 

     self.contentMode = UIViewContentModeScaleAspectFit; 
     self.userInteractionEnabled=YES; 

     singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onSingleTap:)]; 
     singleTapRecognizer.numberOfTapsRequired = 1; 
     singleTapRecognizer.numberOfTouchesRequired = 1; 

     [self addGestureRecognizer:singleTapRecognizer]; 

     [[NSURLConnection alloc] initWithRequest:[NSURLRequest requestWithURL:_url] delegate:self]; 
    } 

    return self; 
} 
+0

謝謝viruss ...我在這裏犯了這樣一個簡單的錯誤。我認爲這是由代表們造成的。 – Melissa

+0

高興地幫助你.. :) –