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
的子層觸摸?可能嗎?
謝謝!
我想在您建議的代碼我的'viewDidLoad'並製作了「yourLayer」'starLayer'。我得到了錯誤「CALayer'沒有可見的@interface聲明選擇器'addGestureRecognizer:' 'starLayer'不是什麼應該代替」yourLayer「嗎? – Xeaza
你在頭文件中有什麼?假設:UIWebView *您必須編寫:[self.webview addGestureRec ...]; – TonyMkenu
在我的頭文件中,我有'@property(weak,nonatomic)IBOutlet UIWebView * aboutWebView;' 我添加了self.aboutWebView addGestureRec ...以及您的其他建議的代碼,它仍然沒有做任何事......嗯 – Xeaza