Skip to content

Instantly share code, notes, and snippets.

@lyokato
Created September 10, 2013 05:38
Show Gist options
  • Save lyokato/6505381 to your computer and use it in GitHub Desktop.
Save lyokato/6505381 to your computer and use it in GitHub Desktop.
UIImage *image = [UIImage imageNamed:@"hoge.png"];
UIImageView *view = [[UIImageView alloc] initWithImage:image];
[view scaleToSize:image.size];
#import "UIView+LKViewUtil.h"
#import "LKMacro.h"
#import <QuartzCore/QuartzCore.h>
@implementation UIView (LKViewUtil)
- (void)centerizeInRect:(CGRect)rect
{
self.frame = CGRectMake((rect.size.width - self.frame.size.width)/2.0,
(rect.size.height - self.frame.size.height)/2.0,
self.frame.size.width,
self.frame.size.height);
}
- (void)centerizeInWidth:(CGFloat)width
{
self.frame = CGRectMake((width - self.frame.size.width)/2.0,
self.frame.origin.y,
self.frame.size.width,
self.frame.size.height);
}
- (void)centerizeInHeight:(CGFloat)height
{
self.frame = CGRectMake(self.frame.origin.x,
(height - self.frame.size.height)/2.0,
self.frame.size.width,
self.frame.size.height);
}
- (void)scaleToSize:(CGSize)size
{
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, size.width, size.height);
}
- (void)scaleToW:(CGFloat)w H:(CGFloat)h
{
self.frame = CGRectMake(self.frame.origin.x,self.frame.origin.y,w,h);
}
- (void)scaleToW:(CGFloat)w
{
self.frame = CGRectMake(self.frame.origin.x,self.frame.origin.y,w,self.frame.size.height);
}
- (void)scaleToH:(CGFloat)h
{
self.frame = CGRectMake(self.frame.origin.x,self.frame.origin.y,self.frame.size.width,h);
}
- (void)moveByX:(CGFloat)x Y:(CGFloat)y
{
self.frame = CGRectMake(self.frame.origin.x + x,
self.frame.origin.y + y,
self.frame.size.width,
self.frame.size.height);
}
- (void)moveBy:(CGPoint)offset
{
self.frame = CGRectMake(self.frame.origin.x + offset.x,
self.frame.origin.y + offset.y,
self.frame.size.width,
self.frame.size.height);
}
- (void)moveToX:(CGFloat)x
{
self.frame = CGRectMake(x, self.frame.origin.y, self.frame.size.width, self.frame.size.height);
}
- (void)moveToY:(CGFloat)y
{
self.frame = CGRectMake(self.frame.origin.x, y, self.frame.size.width, self.frame.size.height);
}
- (void)moveToX:(CGFloat)x Y:(CGFloat)y
{
self.frame = CGRectMake(x, y, self.frame.size.width, self.frame.size.height);
}
- (void)moveTo:(CGPoint)point
{
self.frame = CGRectMake(point.x, point.y, self.frame.size.width, self.frame.size.height);
}
- (void)pulsateWithDuration:(CGFloat)duration
count:(NSInteger)count
{
self.alpha = 0.0;
__weak typeof(self) wself = self;
[UIView animateWithDuration:duration
delay:0.0
options:UIViewAnimationOptionRepeat|UIViewAnimationOptionCurveLinear
animations:^{
[UIView setAnimationRepeatCount:count];
[UIView setAnimationRepeatAutoreverses:YES];
wself.alpha = 1.0;
} completion:^(BOOL finished) {
//[wself cancelPulsatingWithDuration:duration];
}];
}
- (void)cancelPulsatingWithDuration:(CGFloat)duration
{
__weak typeof(self) wself = self;
[UIView animateWithDuration:duration
delay:0.1
options:UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionCurveEaseOut
animations:^{
[UIView setAnimationRepeatCount:1];
wself.alpha = 1.0;
} completion:^(BOOL finished) {
}];
}
- (void)shadowWithColor:(UIColor *)color
radius:(CGFloat)radius
offset:(CGSize)offset
opacity:(CGFloat)opacity
{
//wrap.backgroundColor = [UIColor clearColor];
self.layer.shadowColor = color.CGColor;
self.layer.shadowRadius = radius;
self.layer.shadowOffset = offset;
self.layer.shadowOpacity = opacity;
self.layer.shadowPath =
[UIBezierPath bezierPathWithRoundedRect:self.bounds
cornerRadius:self.layer.cornerRadius].CGPath;
self.layer.shouldRasterize = YES;
self.layer.rasterizationScale = [UIScreen mainScreen].scale;
self.layer.contentsScale = [UIScreen mainScreen].scale;
}
+ (UIView *)shadowViewWrappingView:(UIView *)view
withColor:(UIColor *)color
radius:(CGFloat)radius
offset:(CGSize)offset
opacity:(CGFloat)opacity
{
/*
view.layer.masksToBounds = YES;
view.layer.opaque = NO;
view.layer.cornerRadius = cornerRadius;
*/
view.frame = (CGRect){CGPointZero, view.bounds.size};
UIView *wrap = [[[UIView alloc] initWithFrame:view.bounds] autorelease];
wrap.backgroundColor = [UIColor clearColor];
wrap.layer.shadowColor = color.CGColor;
wrap.layer.shadowRadius = radius;
wrap.layer.shadowOffset = offset;
wrap.layer.shadowOpacity = opacity;
wrap.layer.shadowPath =
[UIBezierPath bezierPathWithRoundedRect:view.bounds
cornerRadius:view.layer.cornerRadius].CGPath;
wrap.layer.shouldRasterize = YES;
wrap.layer.rasterizationScale = [UIScreen mainScreen].scale;
wrap.layer.contentsScale = [UIScreen mainScreen].scale;
[wrap addSubview:view];
return wrap;
}
- (UIImage*)screenshot
{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);
[self.layer renderInContext: UIGraphicsGetCurrentContext()];
UIImage *ss = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return ss;
}
// borrowed from triaina
- (UIViewController *)viewController
{
for (UIView *next = [self superview]; next; next = next.superview) {
UIResponder *nextResponder = [next nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]]) {
return (UIViewController *)nextResponder;
}
}
return nil;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment