+ Selection is highlighted when moved.

This commit is contained in:
Moritz Haarmann
2014-04-28 01:31:39 +02:00
parent fb271e8acb
commit ab2ec51204
4 changed files with 50 additions and 7 deletions

View File

@@ -12,6 +12,7 @@
@property (nonatomic, strong) CALayer* imageLayer; @property (nonatomic, strong) CALayer* imageLayer;
@property (nonatomic, strong) UIImage* scaledImage; @property (nonatomic, strong) UIImage* scaledImage;
@property (nonatomic, strong) CALayer* selectedLayer;
@end @end
@@ -25,6 +26,13 @@
self.clipsToBounds = YES; self.clipsToBounds = YES;
self.imageLayer = [CALayer layer]; self.imageLayer = [CALayer layer];
[self.layer addSublayer:self.imageLayer]; [self.layer addSublayer:self.imageLayer];
self.selectedLayer = [CALayer layer];
[self.layer insertSublayer:self.selectedLayer above:self.imageLayer];
self.selectedLayer.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.5f].CGColor;
self.selectedLayer.opacity = 0.6f;
self.clipsToBounds = YES;
} }
return self; return self;
} }
@@ -33,6 +41,7 @@
CGSize size = CGSizeMake(sourceImage.size.width/30, sourceImage.size.height/30); CGSize size = CGSizeMake(sourceImage.size.width/30, sourceImage.size.height/30);
UIGraphicsBeginImageContext(size); UIGraphicsBeginImageContext(size);
[sourceImage drawInRect:CGRectMake(0, 0, size.width, size.height)]; [sourceImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
self.scaledImage = UIGraphicsGetImageFromCurrentImageContext(); self.scaledImage = UIGraphicsGetImageFromCurrentImageContext();
self.imageLayer.shouldRasterize = YES; self.imageLayer.shouldRasterize = YES;
@@ -40,11 +49,19 @@
self.imageLayer.magnificationFilter = kCAFilterNearest; self.imageLayer.magnificationFilter = kCAFilterNearest;
self.imageLayer.contents = (id)self.scaledImage.CGImage; self.imageLayer.contents = (id)self.scaledImage.CGImage;
UIGraphicsEndImageContext(); UIGraphicsEndImageContext();
} }
- (void)setSelected:(BOOL)selected {
self->_selected = selected;
if (selected){
self.selectedLayer.opacity = 0.6f;
} else {
self.selectedLayer.opacity = 0.0f;
}
}
- (void)layoutSubviews { - (void)layoutSubviews {
[super layoutSubviews]; [super layoutSubviews];
@@ -54,6 +71,7 @@
self.imageLayer.frame = self.imageFrame; self.imageLayer.frame = self.imageFrame;
self.imageLayer.masksToBounds = YES; self.imageLayer.masksToBounds = YES;
self.selectedLayer.frame= self.bounds;
[CATransaction commit]; [CATransaction commit];
} }

View File

@@ -8,11 +8,17 @@
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
@interface BITImageAnnotation : UIView @interface BITImageAnnotation : UIView {
BOOL _selected;
}
@property (nonatomic) CGSize movedDelta; @property (nonatomic) CGSize movedDelta;
@property (nonatomic, weak) UIImage *sourceImage; @property (nonatomic, weak) UIImage *sourceImage;
@property (nonatomic) CGRect imageFrame; @property (nonatomic) CGRect imageFrame;
-(BOOL)resizable; -(BOOL)resizable;
- (void)setSelected:(BOOL)selected;
- (BOOL)isSelected;
@end @end

View File

@@ -25,4 +25,12 @@
return NO; return NO;
} }
- (void)setSelected:(BOOL)selected {
self->_selected = selected;
}
- (BOOL)isSelected {
return self->_selected;
}
@end @end

View File

@@ -161,7 +161,13 @@
#pragma mark - Gesture Handling #pragma mark - Gesture Handling
- (void)panned:(UIPanGestureRecognizer *)gestureRecognizer { - (void)panned:(UIPanGestureRecognizer *)gestureRecognizer {
if ([self.editingControls selectedSegmentIndex] != UISegmentedControlNoSegment || self.isDrawing ){ BITImageAnnotation *annotationAtLocation = (BITImageAnnotation *)[self.view hitTest:[gestureRecognizer locationInView:self.view] withEvent:nil];
if (![annotationAtLocation isKindOfClass:[BITImageAnnotation class]]){
annotationAtLocation = nil;
}
if (([self.editingControls selectedSegmentIndex] != UISegmentedControlNoSegment || self.isDrawing) && !annotationAtLocation ){
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];
@@ -192,11 +198,14 @@
} else { } else {
if (gestureRecognizer.state == UIGestureRecognizerStateBegan){ if (gestureRecognizer.state == UIGestureRecognizerStateBegan){
// find and possibly move an existing annotation. // find and possibly move an existing annotation.
BITImageAnnotation *selectedAnnotation = (BITImageAnnotation *)[self.view hitTest:[gestureRecognizer locationInView:self.view] withEvent:nil];
if ([self.objects indexOfObject:selectedAnnotation] != NSNotFound){ if ([self.objects indexOfObject:annotationAtLocation] != NSNotFound){
self.currentAnnotation = selectedAnnotation; self.currentAnnotation = annotationAtLocation;
[annotationAtLocation setSelected:YES];
} }
} else if (gestureRecognizer.state == UIGestureRecognizerStateChanged && self.currentAnnotation){ } else if (gestureRecognizer.state == UIGestureRecognizerStateChanged && self.currentAnnotation){
CGPoint delta = [gestureRecognizer translationInView:self.view]; CGPoint delta = [gestureRecognizer translationInView:self.view];
@@ -213,6 +222,8 @@
} else { } else {
self.currentAnnotation = nil; self.currentAnnotation = nil;
[annotationAtLocation setSelected:NO];
} }
} }
} }