I am new for CAShapeLayer
.In my application I am showing subjects (CAShapeLayer
) in scrollview.When user click on subject i want to show exams and set content size of scrollview.I am finding touch point and and then comparing with each subject.If match,show exams.When i click on subject and try to change position,it is showing me twice,while i am adding only once.Here is code of my touch event:
- (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];
}
}
}
}