Skip to content

Instantly share code, notes, and snippets.

@DevAlloy
Created December 11, 2013 13:42
Show Gist options
  • Save DevAlloy/7910637 to your computer and use it in GitHub Desktop.
Save DevAlloy/7910637 to your computer and use it in GitHub Desktop.
#import "ViewController.h"
// "Change size" button const
#define BUTTON_WIDTH 120
#define BUTTON_HEIGHT 40
#define IMAGE_CAP_INSET_TOP 30.0
#define IMAGE_CAP_INSET_LEFT 30.0
#define IMAGE_CAP_INSET_BOTTOM 30.0
#define IMAGE_CAP_INSET_RIGHT 30.0
@interface ViewController ()
@property (strong, nonatomic) UIImageView *ballonImageView;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewWillAppear:(BOOL)animated{
// Create button
UIButton *changeSizeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[changeSizeButton setFrame:CGRectMake((self.view.frame.size.width - BUTTON_WIDTH)/2, self.view.frame.size.height - BUTTON_HEIGHT, BUTTON_WIDTH, BUTTON_HEIGHT)];
[changeSizeButton setTitle:@"Change Size" forState:UIControlStateNormal];
[changeSizeButton addTarget:self action:@selector(changeBallonImageSize) forControlEvents:UIControlEventTouchUpInside];
// Create imageViewForBallon
_ballonImageView = [[UIImageView alloc] initWithFrame:[self randomRect]];
UIImage* balloonImage = [self balloonImageWithRect:_ballonImageView.frame];
[_ballonImageView setImage:balloonImage];
[self.view addSubview:changeSizeButton];
[self.view addSubview:_ballonImageView];
}
- (UIImage *)balloonImageWithRect:(CGRect)rect{
//cap corners
UIImage *ballonImage = [[UIImage imageNamed:@"balloon"] resizableImageWithCapInsets:UIEdgeInsetsMake(IMAGE_CAP_INSET_TOP, IMAGE_CAP_INSET_LEFT, IMAGE_CAP_INSET_BOTTOM, IMAGE_CAP_INSET_RIGHT) resizingMode:UIImageResizingModeStretch];
UIImage *arrowImage = [UIImage imageNamed:@"arrow"];
//drawing area
CGSize newSize = rect.size;
UIGraphicsBeginImageContext(newSize);
//leave some space for arrow at the bottom
[ballonImage drawInRect:CGRectMake(0, 0, newSize.width, newSize.height - arrowImage.size.height)];
//draw arrow at the bottom
[arrowImage drawInRect:CGRectMake((newSize.width - arrowImage.size.width)/2, newSize.height - arrowImage.size.height, arrowImage.size.width, arrowImage.size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
- (void) changeBallonImageSize {
[_ballonImageView setFrame:[self randomRect]];
UIImage *newImage = [self balloonImageWithRect:_ballonImageView.frame];
[_ballonImageView setImage:newImage];
}
// ugly code for random rect :(
// sometemes causes exeption when return rect with 0 width or height
- (CGRect) randomRect {
// actually it's random in proper bounds
CGPoint leftTop = [self randomPointInRect:CGRectMake(10, 20, self.view.frame.size.width - 120, self.view.frame.size.height - 120)];
CGPoint rightBot = [self randomPointInRect:CGRectMake(leftTop.x + 100, leftTop.y + 100, self.view.frame.size.width - leftTop.x - 110, self.view.frame.size.height - leftTop.y - 110)];
// create rect with this two points
return CGRectMake(leftTop.x, leftTop.y, rightBot.x - leftTop.x, rightBot.y - leftTop.y);
}
- (CGPoint)randomPointInRect:(CGRect)r
{
CGPoint p = r.origin;
p.x += arc4random_uniform(r.size.width);
p.y += arc4random_uniform(r.size.height);
return p;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc{
_ballonImageView = nil;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment