#import <UIKit/UIKit.h>
@interface UIView (MotionEffect)
//UIMotionEffect和Home页背景视差效果
- (void) addCenterMotionEffectsXYWithOffset:(CGFloat)offset;
@end
@interface UIView (Window)
//把View加在Window上
- (void) addToWindowWithAnimation:(void(^)())animation;
@end
@interface UIView (Screenshot)
//View截图
- (UIImage*) screenshot;
//ScrollView截图 contentOffset
- (UIImage*) screenshotForScrollViewWithContentOffset:(CGPoint)contentOffset;
//View按Rect截图
- (UIImage*) screenshotInFrame:(CGRect)frame;
@end
#import "UIView+Category.h"
#import <QuartzCore/QuartzCore.h>
#import <objc/runtime.h>
@implementation UIView (MotionEffect)
NSString *const centerX = @"center.x";
NSString *const centerY = @"center.y";
#pragma mark - Motion Effect
- (void)addCenterMotionEffectsXYWithOffset:(CGFloat)offset {
// if(!IS_OS_7_OR_LATER) return;
if(floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) return;
UIInterpolatingMotionEffect *xAxis;
UIInterpolatingMotionEffect *yAxis;
xAxis = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:centerX
type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
xAxis.maximumRelativeValue = @(offset);
xAxis.minimumRelativeValue = @(-offset);
yAxis = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:centerY
type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
yAxis.minimumRelativeValue = @(-offset);
yAxis.maximumRelativeValue = @(offset);
UIMotionEffectGroup *group = [[UIMotionEffectGroup alloc] init];
group.motionEffects = @[xAxis, yAxis];
[self addMotionEffect:group];
}
@end
@implementation UIView (AddToWindow)
- (void) addToWindowWithAnimation:(void(^)())animation
{
id appDelegate = [[UIApplication sharedApplication] delegate];
if ([appDelegate respondsToSelector:@selector(window)])
{
UIWindow * window = (UIWindow *) [appDelegate performSelector:@selector(window)];
[window addSubview:self];
[UIView animateWithDuration:0.3 animations:^{
animation();
} completion:^(BOOL finished) {
}];
}
}
@end
@implementation UIView (Screenshot)
- (UIImage*) screenshot {
UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// helps w/ our colors when blurring
// feel free to adjust jpeg quality (lower = higher perf)
NSData *imageData = UIImageJPEGRepresentation(image, 0.75);
image = [UIImage imageWithData:imageData];
return image;
}
- (UIImage *) screenshotForScrollViewWithContentOffset:(CGPoint)contentOffset {
UIGraphicsBeginImageContext(self.bounds.size);
//need to translate the context down to the current visible portion of the scrollview
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0.0f, -contentOffset.y);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// helps w/ our colors when blurring
// feel free to adjust jpeg quality (lower = higher perf)
NSData *imageData = UIImageJPEGRepresentation(image, 0.55);
image = [UIImage imageWithData:imageData];
return image;
}
- (UIImage*) screenshotInFrame:(CGRect)frame {
UIGraphicsBeginImageContext(frame.size);
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), frame.origin.x, frame.origin.y);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// helps w/ our colors when blurring
// feel free to adjust jpeg quality (lower = higher perf)
NSData *imageData = UIImageJPEGRepresentation(image, 0.75);
image = [UIImage imageWithData:imageData];
return image;
}
@end
网友评论