+ Improved Selection Handling in the annotation view controller

This commit is contained in:
moritz haarmann
2014-04-28 11:07:48 +02:00
parent ab2ec51204
commit ce835a0cee
2 changed files with 69 additions and 12 deletions

View File

@@ -26,12 +26,12 @@
self = [super initWithFrame:frame]; self = [super initWithFrame:frame];
if (self) { if (self) {
self.shapeLayer = [CAShapeLayer layer]; self.shapeLayer = [CAShapeLayer layer];
self.shapeLayer.strokeColor = [UIColor redColor].CGColor; self.shapeLayer.strokeColor = [UIColor whiteColor].CGColor;
self.shapeLayer.lineWidth = 5; self.shapeLayer.lineWidth = 5;
self.shapeLayer.fillColor = [UIColor clearColor].CGColor; self.shapeLayer.fillColor = [UIColor redColor].CGColor;
self.strokeLayer = [CAShapeLayer layer]; self.strokeLayer = [CAShapeLayer layer];
self.strokeLayer.strokeColor = [UIColor whiteColor].CGColor; self.strokeLayer.strokeColor = [UIColor redColor].CGColor;
self.strokeLayer.lineWidth = 10; self.strokeLayer.lineWidth = 10;
self.strokeLayer.fillColor = [UIColor clearColor].CGColor; self.strokeLayer.fillColor = [UIColor clearColor].CGColor;
[self.layer addSublayer:self.strokeLayer]; [self.layer addSublayer:self.strokeLayer];
@@ -154,4 +154,39 @@
return (CGAffineTransform){ cosine, sine, -sine, cosine, startPoint.x, startPoint.y }; return (CGAffineTransform){ cosine, sine, -sine, cosine, startPoint.x, startPoint.y };
} }
#pragma mark - UIView
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIColor *color = [self colorAtPoint:point];
CGFloat alpha, white;
[color getWhite:&white alpha:&alpha];
if (white || alpha){
return self;
} else {
return nil;
}
}
#pragma mark - Helpers
// This is taken from http://stackoverflow.com/questions/12770181/how-to-get-the-pixel-color-on-touch
- (UIColor *)colorAtPoint:(CGPoint)point {
unsigned char pixel[4] = {0};
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pixel,
1, 1, 8, 4, colorSpace, (CGBitmapInfo)kCGImageAlphaPremultipliedLast);
CGContextTranslateCTM(context, -point.x, -point.y);
[self.layer renderInContext:context];
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
UIColor *color = [UIColor colorWithRed:pixel[0]/255.0
green:pixel[1]/255.0 blue:pixel[2]/255.0
alpha:pixel[3]/255.0];
return color;
}
@end @end

View File

@@ -14,6 +14,12 @@
#import "BITHockeyHelper.h" #import "BITHockeyHelper.h"
#import "HockeySDKPrivate.h" #import "HockeySDKPrivate.h"
typedef NS_ENUM(NSInteger, BITImageAnnotationViewControllerInteractionMode) {
BITImageAnnotationViewControllerInteractionModeNone,
BITImageAnnotationViewControllerInteractionModeDraw,
BITImageAnnotationViewControllerInteractionModeMove
};
@interface BITImageAnnotationViewController () @interface BITImageAnnotationViewController ()
@property (nonatomic, strong) UIImageView *imageView; @property (nonatomic, strong) UIImageView *imageView;
@@ -29,7 +35,7 @@
@property (nonatomic) CGPoint panStart; @property (nonatomic) CGPoint panStart;
@property (nonatomic,strong) BITImageAnnotation *currentAnnotation; @property (nonatomic,strong) BITImageAnnotation *currentAnnotation;
@property (nonatomic) BOOL isDrawing; @property (nonatomic) BITImageAnnotationViewControllerInteractionMode currentInteraction;
@property (nonatomic) CGRect pinchStartingFrame; @property (nonatomic) CGRect pinchStartingFrame;
@@ -158,7 +164,7 @@
return renderedImageOfMyself; return renderedImageOfMyself;
} }
#pragma mark - Gesture Handling #pragma mark - UIGestureRecognizers
- (void)panned:(UIPanGestureRecognizer *)gestureRecognizer { - (void)panned:(UIPanGestureRecognizer *)gestureRecognizer {
BITImageAnnotation *annotationAtLocation = (BITImageAnnotation *)[self.view hitTest:[gestureRecognizer locationInView:self.view] withEvent:nil]; BITImageAnnotation *annotationAtLocation = (BITImageAnnotation *)[self.view hitTest:[gestureRecognizer locationInView:self.view] withEvent:nil];
@@ -167,7 +173,22 @@
annotationAtLocation = nil; annotationAtLocation = nil;
} }
if (([self.editingControls selectedSegmentIndex] != UISegmentedControlNoSegment || self.isDrawing) && !annotationAtLocation ){ // determine the interaction mode if none is set so far.
if (self.currentInteraction == BITImageAnnotationViewControllerInteractionModeNone){
if (annotationAtLocation){
self.currentInteraction = BITImageAnnotationViewControllerInteractionModeMove;
} else if ([self canDrawNewAnnotation]){
self.currentInteraction = BITImageAnnotationViewControllerInteractionModeDraw;
}
}
if (self.currentInteraction == BITImageAnnotationViewControllerInteractionModeNone){
return;
}
if (self.currentInteraction == BITImageAnnotationViewControllerInteractionModeDraw){
if (gestureRecognizer.state == UIGestureRecognizerStateBegan){ if (gestureRecognizer.state == UIGestureRecognizerStateBegan){
self.currentAnnotation = [self annotationForCurrentMode]; self.currentAnnotation = [self annotationForCurrentMode];
[self.objects addObject:self.currentAnnotation]; [self.objects addObject:self.currentAnnotation];
@@ -182,7 +203,6 @@
self.panStart = [gestureRecognizer locationInView:self.imageView]; self.panStart = [gestureRecognizer locationInView:self.imageView];
// [self.editingControls setSelectedSegmentIndex:UISegmentedControlNoSegment]; // [self.editingControls setSelectedSegmentIndex:UISegmentedControlNoSegment];
self.isDrawing = YES;
} else if (gestureRecognizer.state == UIGestureRecognizerStateChanged){ } else if (gestureRecognizer.state == UIGestureRecognizerStateChanged){
CGPoint bla = [gestureRecognizer locationInView:self.imageView]; CGPoint bla = [gestureRecognizer locationInView:self.imageView];
@@ -193,9 +213,9 @@
[self.currentAnnotation layoutIfNeeded]; [self.currentAnnotation layoutIfNeeded];
} else { } else {
self.currentAnnotation = nil; self.currentAnnotation = nil;
self.isDrawing = NO; self.currentInteraction = BITImageAnnotationViewControllerInteractionModeNone;
} }
} else { } else if (self.currentInteraction == BITImageAnnotationViewControllerInteractionModeMove){
if (gestureRecognizer.state == UIGestureRecognizerStateBegan){ if (gestureRecognizer.state == UIGestureRecognizerStateBegan){
// find and possibly move an existing annotation. // find and possibly move an existing annotation.
@@ -223,6 +243,8 @@
} else { } else {
self.currentAnnotation = nil; self.currentAnnotation = nil;
[annotationAtLocation setSelected:NO]; [annotationAtLocation setSelected:NO];
self.currentInteraction = BITImageAnnotationViewControllerInteractionModeNone;
} }
} }
@@ -281,15 +303,12 @@
-(void)tapped:(UIGestureRecognizer *)tapRecognizer { -(void)tapped:(UIGestureRecognizer *)tapRecognizer {
if (self.navigationController.navigationBarHidden){ if (self.navigationController.navigationBarHidden){
// [[UIApplication sharedApplication] setStatusBarHidden:NO];
[UIView animateWithDuration:0.35f animations:^{ [UIView animateWithDuration:0.35f animations:^{
self.navigationController.navigationBar.alpha = 1; self.navigationController.navigationBar.alpha = 1;
} completion:^(BOOL finished) { } completion:^(BOOL finished) {
[self fitImageViewFrame]; [self fitImageViewFrame];
[self.navigationController setNavigationBarHidden:NO animated:NO]; [self.navigationController setNavigationBarHidden:NO animated:NO];
[[UIApplication sharedApplication] setStatusBarHidden:NO]; [[UIApplication sharedApplication] setStatusBarHidden:NO];
}]; }];
} else { } else {
[UIView animateWithDuration:0.35f animations:^{ [UIView animateWithDuration:0.35f animations:^{
@@ -319,4 +338,7 @@
return self.imageView; return self.imageView;
} }
- (BOOL)canDrawNewAnnotation {
return [self.editingControls selectedSegmentIndex] != UISegmentedControlNoSegment;
}
@end @end