我是新手CAShapeLayer
。在我的应用程序中,我在滚动视图中显示主题(CAShapeLayer
)。当用户单击主题时,我想显示考试并设置滚动视图的内容大小。我正在寻找接触点,然后与每个主题进行比较。如果匹配,显示考试。当我单击主题并尝试更改位置时,它显示我两次,而我只添加一次。这是我的触摸事件的代码:
- (void)singleTapGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer{
CGPoint touchPoint=[gestureRecognizer locationInView:self];
//take subviews of view
NSArray* subLayerArray = [self subLayerOfView];
BOOL showChildren = NO;
//compare touch point
for (int i = 0; i < subLayerArray.count; i++) {
A3AddSubjectView* aAddSubject = [subLayerArray objectAtIndex:i];
if (showChildren == YES) {
int heightForSubject = 150*(i+1);
aAddSubject.position = CGPointMake(aAddSubject.position.x, heightForSubject);
aAddSubject.backgroundColor = [UIColor redColor].CGColor;
}
CGAffineTransform transf = CGAffineTransformMakeTranslation(-aAddSubject.position.x, - aAddSubject.position.y);
if(CGPathContainsPoint(aAddSubject.path, &transf, touchPoint, NO)){
if (aAddSubject.level == 0) {
aAddSubject.level = 1;
}
else
aAddSubject.level = 0;
showChildren = YES;
[self.delegate didSingleTapSubjectAtLevel:aAddSubject.level withIndex:i inView:self];
NSDate* startDateOfSubject = [aAddSubject.startDate dateByAddingTimeInterval:19800];
aAddSubject = [self.datasource selectSubjectAtLevel:aAddSubject.level withIndex:i inView:self];
for (int j = 0; j < aAddSubject.children.count; j++) {
NSMutableDictionary* dict = [aAddSubject.children objectAtIndex:j];
NSDate* startDateOfExam = [[dict objectForKey:@"StartDate"] dateByAddingTimeInterval:19800];
NSDate* endDateOfExam = [[dict objectForKey:@"EndDate"] dateByAddingTimeInterval:19800];
int totalDaysBetweenExam = [self differenceBetweenTwoDatesFromDate:startDateOfExam toDate:endDateOfExam];
CGFloat widthOfexam = totalDaysBetweenExam * dayWidth;
//size
CGSize sizeOfExam;
sizeOfExam.width = widthOfexam;
sizeOfExam.height = 10;
//distance from origin
int distanceFromOrigin = [self differenceBetweenTwoDatesFromDate:startDateOfSubject toDate:startDateOfExam] * dayWidth;
A3ExamsView* aExamView = [[A3ExamsView alloc]init];
[aExamView examWithTitle:[dict objectForKey:@"title"] withFont:[UIFont systemFontOfSize:20] withSize:sizeOfExam];
aExamView.position = CGPointMake(distanceFromOrigin, 70*(j+1));
[self.layer addSublayer:aExamView];
}
}
}
}