Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
SD Image Map User Control - GeneXus X Ev2 - iOS
#import "GXControlGridBase.h"
@interface GXControlImageMap : GXControlGridBase {
}
@end
#import "GXControlImageMap.h"
#import "GXObjectHelper.h"
#import "GXResources.h"
#import "UIImageView+WebCache.h"
@interface GXControlImageMap (UserControlProperties)
- (NSString *)imageName;
- (NSString *)imageAtt;
- (NSString *)imageField;
- (NSString *)horizontalCoordinateAtt;
- (NSString *)horizontalCoordinateField;
- (NSString *)verticalCoordinateAtt;
- (NSString *)verticalCoordinateField;
- (NSString *)sizeAtt;
- (NSString *)sizeField;
@end
#pragma mark -
@interface GXControlImageMap (Private)
- (void)loadData:(NSInteger)count;
- (CGRect)transformedRect:(CGRect)frame;
@end
#pragma mark -
@implementation GXControlImageMap
- (UIImageView *)imageView {
return (UIImageView *)[self gridView];
}
#pragma mark - User Control Properties
- (NSString *)imageName {
NSString *imageName = [[self properties] getPropertyValueString:@"@SDImageMapImage"];
imageName = [GXObjectHelper parseObjectNameOfType:kGXObjectIdImage from:imageName];
return imageName;
}
- (NSString *)imageAtt {
return [[self properties] getPropertyValueString:@"@SDImageMapImageAtt"];
}
- (NSString *)imageField {
return [[self properties] getPropertyValueString:@"@SDImageMapImageField"];
}
- (NSString *)horizontalCoordinateAtt {
return [[self properties] getPropertyValueString:@"@SDImageMapHCoordAtt"];
}
- (NSString *)horizontalCoordinateField {
return [[self properties] getPropertyValueString:@"@SDImageMapHCoordField"];
}
- (NSString *)verticalCoordinateAtt {
return [[self properties] getPropertyValueString:@"@SDImageMapVCoordAtt"];
}
- (NSString *)verticalCoordinateField {
return [[self properties] getPropertyValueString:@"@SDImageMapVCoordField"];
}
- (NSString *)sizeAtt {
return [[self properties] getPropertyValueString:@"@SDImageMapSizeAtt"];
}
- (NSString *)sizeField {
return [[self properties] getPropertyValueString:@"@SDImageMapSizeField"];
}
#pragma mark - GXControlGridBase Overrides
- (UIView *)newGridViewWithFrame:(CGRect)frame {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
[imageView setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
NSString *imageName = [self imageName];
if (imageName) {
UIImage *embededImage = [GXResources imageForName:imageName];
if (embededImage) {
[imageView setImage:embededImage];
}
else {
NSURL *imageUrl = [GXResources urlForImageName:imageName];
[imageView setImageWithURL:imageUrl placeholderImage:nil];
}
}
[imageView setUserInteractionEnabled:YES];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleImageTap:)];
[imageView addGestureRecognizer:tapGestureRecognizer];
[tapGestureRecognizer release];
return imageView;
}
- (void)reloadData {
[self loadData:[[self entityDataListProvider] numberOfLoadedEntitiesInSection:0]];
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[self reloadData];
}
#pragma mark - Private
- (void)loadData:(NSInteger)count {
[[[self imageView] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
if (!count) {
return;
}
NSString *imageNameAtt = [self imageAtt];
NSString *imageNameField = [self imageField];
NSString *hCoordAtt = [self horizontalCoordinateAtt];
NSString *hCoordField = [self horizontalCoordinateField];
NSString *vCoordAtt = [self verticalCoordinateAtt];
NSString *vCoordField = [self verticalCoordinateField];
NSString *sizeAtt = [self sizeAtt];
NSString *sizeField = [self sizeField];
BOOL hasDefaultAction = [self canExecuteDefaultActionForEntityAtIndex:0 section:0];
for(NSUInteger index = 0; index < count; index++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
NSString *imageName = [self valueForEntityDataFieldName:imageNameAtt fieldSpecifier:imageNameField indexPath:indexPath];
NSNumber *hCoord = [self valueForEntityDataFieldName:hCoordAtt fieldSpecifier:hCoordField indexPath:indexPath];
NSNumber *vCoord = [self valueForEntityDataFieldName:vCoordAtt fieldSpecifier:vCoordField indexPath:indexPath];
NSNumber *size = [self valueForEntityDataFieldName:sizeAtt fieldSpecifier:sizeField indexPath:indexPath];
CGRect frame = [self transformedRect:CGRectMake([hCoord intValue], [vCoord intValue], [size intValue], [size intValue])];
UIView *itemView;
if (imageName) {
itemView = [[UIImageView alloc] initWithFrame:frame];
UIImage *embededImage = [GXResources imageForName:imageName];
if (embededImage) {
[(UIImageView *)itemView setImage:embededImage];
}
else {
NSURL *imageUrl = [GXResources urlForImageName:imageName];
[(UIImageView *)itemView setImageWithURL:imageUrl placeholderImage:nil];
}
}
else {
itemView = [[UIView alloc] initWithFrame:frame];
}
[itemView setBackgroundColor:[UIColor clearColor]];
[itemView setUserInteractionEnabled:hasDefaultAction];
[itemView setTag:index];
[[self imageView] addSubview:itemView];
[itemView release];
}
}
- (CGRect)transformedRect:(CGRect)frame {
CGSize imgSize = [[self imageView] image].size;
CGSize viewSize = [[self imageView] frame].size;
if (imgSize.height == 0 || imgSize.width == 0) {
return frame;
}
CGFloat xTransform = viewSize.width / imgSize.width;
CGFloat yTransform = viewSize.height / imgSize.height;
return CGRectMake(frame.origin.x * xTransform,
frame.origin.y * yTransform,
frame.size.width * xTransform,
frame.size.height * yTransform
);
}
#pragma mark - Actions
- (void)handleImageTap:(UITapGestureRecognizer *)sender {
UIView *mainView = [self imageView];
CGPoint tapPoint = [sender locationInView:mainView];
UIView *tappedView = [mainView hitTest:tapPoint withEvent:nil];
if (tappedView && tappedView != mainView) {
NSUInteger index = [tappedView tag];
[self executeDefaultActionForEntityAtSection:0 row:index];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment