2013-01-15 25 views
0

我有一個圖層,作爲UIWebView的子圖層添加。我希望能夠觸摸並拖動圖層。在添加UIWebView之前,我能夠輕鬆地在圖層周圍觸摸並拖動。如何調用touchesBegin作爲UIWebView的子圖層

這裏就是我創建圖層,將其添加爲子

- (void)viewDidLoad 
{ 
    [super viewDidLoad];  
    starLayer = [CALayer layer]; 
    starLayer.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage 
                     imageNamed:@"star"]].CGColor; 
    starLayer.shadowOffset = CGSizeMake(0, 3); 
    starLayer.shadowRadius = 5.0; 
    starLayer.shadowColor = [UIColor blackColor].CGColor; 
    starLayer.shadowOpacity = 0.8; 
    starLayer.frame = CGRectMake(235.0, 200.0, 35.0, 35.0); 
    [self.aboutWebView.layer addSublayer:starLayer]; 

    [self moveStar]; 
} 

這裏是我的moveStar方法

- (void)moveStar 
{ 
    CAKeyframeAnimation *move = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 
    NSMutableArray *values = [NSMutableArray array]; 
    [values addObject:[NSValue valueWithCGPoint:CGPointMake(250.0, 50.0)]]; 
    [values addObject:[NSValue valueWithCGPoint:CGPointMake(250.0, 250.0)]]; 
    [move setValues:values]; 
    [move setDuration:1.0]; 
    [move setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]]; 

    // Add the animation to the layer to be animated 
    [starLayer addAnimation:move forKey:@"moveAnimation"]; 

}

到目前爲止好。這顆星落在屏幕上,落在中間的上方。但是當我嘗試觸摸這顆星時,什麼都沒有發生。由於某種原因永遠不會被召喚。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint point = [touch locationInView:self.aboutWebView]; 
    [starLayer setPosition:point]; 
} 

是我不能觸碰時,它的aboutWebView一個子層,因爲觸摸報告給視圖,而不是一個層的明星的原因嗎?

有沒有人有任何建議如何讓明星作爲aboutWebView的子層觸摸?可能嗎?

謝謝!

回答

0

在您的圖層中...嘗試將UIGestureRecognizer屬性cancelsTouchesInView設置爲NO。

「將該值設置爲NO指示識別器,提供全觸控的基本看法,甚至當它已經認識到序列」

類似的東西:

UIGestureRecognizer *touches = [UIGestureRecognizer alloc]; 
    //or 
    //UIGestureRecognizer *touches = [[UIGestureRecognizer alloc] initWithTarget:self action:@selector(touchesAction:)]; 

    touches.enabled=YES; 
    [yourLayer addGestureRecognizer:touches]; 
    touches.cancelsTouchesInView=NO; 
+0

我想在您建議的代碼我的'viewDidLoad'並製作了「yourLayer」'starLayer'。我得到了錯誤「CALayer'沒有可見的@interface聲明選擇器'addGestureRecognizer:' 'starLayer'不是什麼應該代替」yourLayer「嗎? – Xeaza

+0

你在頭文件中有什麼?假設:UIWebView *您必須編寫:[self.webview addGestureRec ...]; – TonyMkenu

+0

在我的頭文件中,我有'@property(weak,nonatomic)IBOutlet UIWebView * aboutWebView;' 我添加了self.aboutWebView addGestureRec ...以及您的其他建議的代碼,它仍然沒有做任何事......嗯 – Xeaza

相關問題