diff --git a/Demo/JCTiledViewDemo/RootViewController.m b/Demo/JCTiledViewDemo/RootViewController.m index 3da1a429..9ec5df65 100644 --- a/Demo/JCTiledViewDemo/RootViewController.m +++ b/Demo/JCTiledViewDemo/RootViewController.m @@ -19,6 +19,11 @@ #import "JCTiledView.h" #endif +@interface RootViewController () + +@property (nonatomic, strong) NSArray *bezierPaths; +@end + @implementation RootViewController @@ -67,9 +72,10 @@ - (void)viewDidLoad addButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin; [addButton addTarget:self action:@selector(addRandomAnnotations) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:addButton]; - + [self tiledScrollViewDidZoom:self.scrollView]; //force the detailView to update the frist time [self addRandomAnnotations]; + } - (void)viewDidUnload @@ -115,18 +121,36 @@ - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event - (void)addRandomAnnotations { - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - srand(42); - }); - - CGSize size = SkippingGirlImageSize; - for (int i = 0; i < 5; i++) - { - id a = [[DemoAnnotation alloc] init]; - a.contentPosition = CGPointMake((float)(rand() % (int)size.width), (float)(rand() % (int)size.height)); - [self.scrollView addAnnotation:a]; - } + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + srand(42); + }); + + CGSize size = SkippingGirlImageSize; + + NSMutableArray *paths = [NSMutableArray array]; + + for (int i = 0; i < 6/2; i++) { + + id firstAnnot = [[DemoAnnotation alloc] init]; + CGPoint firstContentPosition = CGPointMake((float)(rand() % (int)size.width), (float)(rand() % (int)size.height)); + firstAnnot.contentPosition = firstContentPosition; + id secondAnnot = [[DemoAnnotation alloc] init]; + CGPoint secondContentPosition = CGPointMake((float)(rand() % (int)size.width), (float)(rand() % (int)size.height)); + secondAnnot.contentPosition = secondContentPosition; + [self.scrollView addAnnotation:firstAnnot]; + [self.scrollView addAnnotation:secondAnnot]; + + + UIBezierPath *path = [UIBezierPath bezierPath]; + [path moveToPoint:CGPointMake(firstContentPosition.x, firstContentPosition.y + 30.f)]; // 30.f is max height of the annotation image + [path addLineToPoint:CGPointMake(secondContentPosition.x, secondContentPosition.y + 30.f)]; // 30.f is max height of the annotation image + path.lineWidth = 3.f; + [paths addObject:path]; + } + + self.bezierPaths = [NSArray arrayWithArray:paths]; + } #pragma mark - JCTiledScrollViewDelegate @@ -169,4 +193,19 @@ - (UIImage *)tiledScrollView:(JCTiledScrollView *)scrollView imageForRow:(NSInte } +-(UIBezierPath *)tiledScrollViewGetBezierPath:(JCTiledScrollView *)scrollView atIndex:(NSInteger)index{ + return [self.bezierPaths objectAtIndex:index]; +} + +- (UIColor *)tiledScrollViewGetPathColor:(JCTiledScrollView *)scrollView atIndex:(NSInteger)index { + //UIColor *color = [UIColor colorWithRed:38.f/255.f green:166.f/255.f blue:91.f/255.f alpha:1.f]; + UIColor *color = [UIColor whiteColor]; + return color; +} + +- (NSInteger)tiledScrollViewGetNumberOfPath:(JCTiledScrollView *)scrollView { + return [self.bezierPaths count]; +} + + @end diff --git a/Headers/JCTiledScrollView.h b/Headers/JCTiledScrollView.h index 80aaa866..7febfdb8 100755 --- a/Headers/JCTiledScrollView.h +++ b/Headers/JCTiledScrollView.h @@ -56,6 +56,10 @@ - (void)tiledScrollView:(JCTiledScrollView *)scrollView didReceiveDoubleTap:(UIGestureRecognizer *)gestureRecognizer; - (void)tiledScrollView:(JCTiledScrollView *)scrollView didReceiveTwoFingerTap:(UIGestureRecognizer *)gestureRecognizer; +- (UIBezierPath *)tiledScrollViewGetBezierPath:(JCTiledScrollView *)scrollView atIndex:(NSInteger)index; +- (UIColor *)tiledScrollViewGetPathColor:(JCTiledScrollView *)scrollView atIndex:(NSInteger)index; +- (NSInteger)tiledScrollViewGetNumberOfPath:(JCTiledScrollView *)scrollView; + @end @interface JCTiledScrollView : UIView diff --git a/JCTiledScrollView/Source/JCTiledScrollView.m b/JCTiledScrollView/Source/JCTiledScrollView.m index 6f430271..523cbd44 100755 --- a/JCTiledScrollView/Source/JCTiledScrollView.m +++ b/JCTiledScrollView/Source/JCTiledScrollView.m @@ -42,6 +42,7 @@ @interface JCTiledScrollView () )annotation @@ -563,4 +569,45 @@ - (void)removeAllAnnotations [self removeAnnotations:[_annotations allObjects]]; } +#pragma mark - PATH +- (void)addPath { + if([self.tiledScrollViewDelegate respondsToSelector:@selector(tiledScrollViewGetBezierPath:)]){ + UIBezierPath *path = [self.tiledScrollViewDelegate tiledScrollViewGetBezierPath:self]; + [self addPath:path andColor:[self.tiledScrollViewDelegate tiledScrollViewGetPathColor:self]]; + } + else if ([self.tiledScrollViewDelegate respondsToSelector:@selector(tiledScrollViewGetNumberOfPath:)]) { + for (CALayer *layer in [_tiledView.layer sublayers]) { + [layer removeFromSuperlayer]; + } + int number = (int)[self.tiledScrollViewDelegate tiledScrollViewGetNumberOfPath:self]; + for (int i = 0; i < number; i++) { + UIColor *color = [UIColor blackColor]; + UIBezierPath *path = nil; + if([self.tiledScrollViewDelegate respondsToSelector:@selector(tiledScrollViewGetBezierPath:atIndex:)]){ + path = [self.tiledScrollViewDelegate tiledScrollViewGetBezierPath:self atIndex:i]; + } + if([self.tiledScrollViewDelegate respondsToSelector:@selector(tiledScrollViewGetPathColor:atIndex:)]){ + color = [self.tiledScrollViewDelegate tiledScrollViewGetPathColor:self atIndex:i]; + } + [self addPath:path andColor:color]; + } + } +} + +- (void)addPath:(UIBezierPath *)path andColor:(UIColor *)color { + if (!self.pathLayers) self.pathLayers = [NSMutableArray array]; + if (path) { + CAShapeLayer *pathLayr = [CAShapeLayer layer]; + pathLayr.anchorPoint = CGPointMake(0.5, 0.5); + pathLayr.frame = self.tiledView.frame; + pathLayr.path = path.CGPath; + pathLayr.strokeColor = (color?color:[UIColor blackColor]).CGColor; + pathLayr.fillColor = [UIColor clearColor].CGColor; + pathLayr.lineWidth = path.lineWidth; + pathLayr.lineJoin = kCALineJoinBevel; + [_tiledView.layer addSublayer:pathLayr]; + [self.pathLayers addObject:pathLayr]; + } +} + @end