新的一天,一個新的問題!UIWebView addSubview:UIImageView在觸摸事件觸發時不顯示
我一直在研究一個應用程序,它需要我在用戶手指觸摸屏幕時顯示一個圓圈。
我跟着tutorial我發現子類UIWindow和my comment解釋如何傳遞這些觸摸,如果你正在使用故事板和ARC。
我使用下面的代碼(其是基於this tutorial),以使觸摸指示符來顯示:
#pragma mark - Touch Events
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
NSArray *subviews = [self.webView subviews];
for (UIView *view in subviews)
{
// Check if view is scrollview
if ([view isKindOfClass:[UserTouchImageView class]])
{
[view removeFromSuperview];
}
}
// Enumerate over all the touches and draw a red dot on the screen where the touches were
[touches enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
// Get a single touch and it's location
UITouch *touch = obj;
CGPoint touchPoint = [touch locationInView:self.webView];
// Add touch indicator
UserTouchImageView *touchView = [[UserTouchImageView alloc] initWithFrame:CGRectMake(touchPoint.x -15, touchPoint.y -15, 30, 30)];
[self.webView addSubview:touchView];
}];
NSLog(@"Touches began");
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
NSArray *subviews = [self.webView subviews];
for (UIView *view in subviews)
{
// Check if view is scrollview
if ([view isKindOfClass:[UserTouchImageView class]])
{
[view removeFromSuperview];
}
}
// Enumerate over all the touches and draw a red dot on the screen where the touches were
[touches enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
// Get a single touch and it's location
UITouch *touch = obj;
CGPoint touchPoint = [touch locationInView:self.webView];
// Draw a red circle where the touch occurred
UserTouchImageView *touchView = [[UserTouchImageView alloc] initWithFrame:CGRectMake(touchPoint.x -15, touchPoint.y -15, 30, 30)];
[self.webView addSubview:touchView];
}];
NSLog(@"Touches moved");
}
- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
NSArray *subviews = [self.webView subviews];
for (UIView *view in subviews)
{
// Check if view is scrollview
if ([view isKindOfClass:[UserTouchImageView class]])
{
[UIView animateWithDuration:0.5f
animations:^{
view.alpha = 0.0;
}
completion:^(BOOL finished) {
[view removeFromSuperview];
}];
}
}
NSLog(@"Touches ended");
}
- (void) touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event {
NSArray *subviews = [self.webView subviews];
for (UIView *view in subviews)
{
// Check if view is scrollview
if ([view isKindOfClass:[UserTouchImageView class]])
{
[view removeFromSuperview];
}
}
NSLog(@"Touches cancelled");
}
UserTouchImageView是的UIImageView的子類,與變更到默認感:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.image = [UIImage imageNamed:@"greyCircle.png"];
self.alpha = 0.5f;
}
return self;
}
我已經測試了這個沒有UIWebview的代碼(用self.view
代替self.webView
),它工作正常,繪製了我點擊和拖動的圓,然後當我釋放按鈕時淡出。
我還成功實例化了UserTouchImageView,並將其添加到viewViewer的viewDidLoad
方法的webView中。
只要我把UIWebview放在上面的代碼中,同一行([self.webView addSubview:touchView];
)就不起作用。我仍然可以將NSLog消息發送到控制檯,但子視圖不會被明顯添加。
任何人都可以幫我找出爲什麼這不會出現?是否還有其他我需要注意的代碼,或者我無法做到這一點的任何原因?
非常感謝!
編輯
我已經上傳我的源至今here (MediaFire)
EDIT 2
我下面添加了兩個方法:
-(void)listWebViewSubViews
{
NSLog(@"BEGIN LISTING SUBVIEWS");
for (UIView *view in [self.webView subviews]) {
NSLog(@"Subview: %@", view.description);
}
NSLog(@"END LISTING SUBVIEWS");
}
-(void)view:(UIView *)view addTouchIndicatorWithCentreAtPoint:(CGPoint)point
{
NSLog(@"View: %@, x: %f, y: %f", view.description, point.x, point.y);
// Add touch indicator
UserTouchImageView *touchView = [[UserTouchImageView alloc] initWithFrame:CGRectMake(point.x -15, point.y -15, 30, 30)];
[view addSubview:touchView];
}
這些被稱爲從UIWebView外部的兩個按鈕,以及內部的的touchesBegan方法:
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
NSArray *subviews = [self.webView subviews];
for (UIView *view in subviews)
{
if ([view isKindOfClass:[UserTouchImageView class]])
{
[view removeFromSuperview];
}
}
[touches enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
UITouch *touch = obj;
CGPoint touchPoint = [touch locationInView:self.webView];
[self view:self.webView addTouchIndicatorWithCentreAtPoint:touchPoint];
[self listWebViewSubViews];
}];
NSLog(@"Touches began");
}
的記錄似乎表明,web視圖通過self.webView
引用WITHIN的觸摸開始從兩個按鈕引用時方法比一個完全獨立的實例。我可以通過按鈕添加多個子視圖,然後列出它們,並且它們都顯示在日誌中,但是當我單擊以在模擬器上觸摸時,這些額外的子視圖都不會列出。
有誰知道任何特殊功能的UIWebView有觸摸事件的重複實例嗎?
首先,檢查你要添加的子視圖不'nil'。然後嘗試添加一個'setNeedsDisplay'調用。這也可能是因爲你在一個街區內打電話。你在塊內嘗試了'NSLog'嗎? – Dustin 2012-07-19 16:55:18
嗨達斯汀,謝謝你的回覆。它完全通過了我,因爲它在塊內部被調用,所以我已經嘗試了你的兩個建議,對象不是'nil','setNeedsDisplay'沒有可見的效果。我也嘗試在調用dispatch_async(dispatch_get_main_queue(),^ {});''時調用'[selv.webView addSubview:touchView];'。那也行不通。我會繼續尋找,謝謝你的幫助! – 2012-07-20 09:36:29