捏合——缩放
#import "AppDelegate.h"
#import "NewHypnosister.h"
@interface AppDelegate ()<UIScrollViewDelegate>
@property (nonatomic, strong) NewHypnosister *hypnosister;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
self.window.rootViewController = [[UIViewController alloc]init];
CGRect screenView = self.window.bounds;
CGRect bigRect = screenView;
bigRect.size.width *= 2.0;
bigRect.size.height *= 2.0;
UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:screenView];
//pagingEnabled设置为NO
[scrollView setPagingEnabled:NO];
//将NewHypnosister实例变量改为属性
self.hypnosister = [[NewHypnosister alloc]initWithFrame:screenView];
//将NewHypnosister对象作为子视图添加到该对象中
[scrollView addSubview:self.hypnosister];
screenView.origin.x += screenView.size.width;
NewHypnosister *secondHypnosister = [[NewHypnosister alloc]initWithFrame:screenView];
[scrollView addSubview:secondHypnosister];
//属性scrollView的contentSize确定缩放范围
scrollView.contentSize = self.window.bounds.size;
//缩放倍数
scrollView.minimumZoomScale = 1.0;
scrollView.maximumZoomScale = 5.0;
//将AppDelegate对象自身设置为scrollView对象的委托
scrollView.delegate = self;
[self.window makeKeyAndVisible];
[self.window addSubview:scrollView];
self.window.backgroundColor = [UIColor whiteColor];
return YES;
}
#import "NewHypnosister.h"
@interface NewHypnosister()
@property (strong, nonatomic)UIColor *circleColor;
@end
@implementation NewHypnosister
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self){
self.backgroundColor = [UIColor clearColor];
self.circleColor = [UIColor lightGrayColor];
}
return self;
}
- (void)drawRect:(CGRect)rect
{
CGRect bounds = self.bounds;
CGPoint center;
center.x = (bounds.origin.x + bounds.size.width)/2.0;
center.y = (bounds.origin.y + bounds.size.height)/2.0;
float maxRadius = hypot(bounds.size.width, bounds.size.height)/2.0;
UIBezierPath *path = [[UIBezierPath alloc]init];
for (float currentRadius = maxRadius; currentRadius > 0; currentRadius -= 20.0) {
[path moveToPoint:CGPointMake(center.x + currentRadius, center.y)];
[path addArcWithCenter:center
radius:currentRadius
startAngle:0.0
endAngle:M_PI*2.0
clockwise:YES];
}
path.lineWidth = 10.0;
// [[UIColor lightGrayColor] setStroke];
[self.circleColor setStroke];
[path stroke];
//设置阴影
CGContextRef currentContext = UIGraphicsGetCurrentContext();
void CGContextShadow(CGContextRef context, CGSize offSet, CGFloat blur);
CGContextSetShadow(currentContext, CGSizeMake(4, 7), 3);
//绘制三角形,顶点(bounds.origin.x + bounds.size.width/2.0, bounds.origin.y),底边为bounds底边
UIBezierPath *myPath = [UIBezierPath bezierPath];
CGPoint myPoint[3];
myPoint[0] = CGPointMake(bounds.origin.x + bounds.size.width/2.0, bounds.origin.y);
myPoint[1] = CGPointMake(bounds.origin.x, bounds.origin.y + bounds.size.height);
myPoint[2] = CGPointMake(bounds.origin.x + bounds.size.width, bounds.origin.y + bounds.size.height);
//绘制三角形
[myPath moveToPoint:myPoint[0]];
[myPath addLineToPoint:myPoint[1]];
[myPath addLineToPoint:myPoint[2]];
[myPath stroke];
//保存剪切三角形及设置颜色渐变
CGContextSaveGState(currentContext);
[myPath addClip];
CGFloat locations[2] = {0.0, 1.0};
CGFloat components[8] = {0.0, 1.0, 0.0, 1.0,//起始颜色为黄色
1.0, 1.0, 0.0, 1.0};//终止颜色为绿色
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorspace, components, locations, 2);
CGPoint startPoint = myPoint[0];
CGPoint endPoint = myPoint[2];
CGContextDrawLinearGradient(currentContext, gradient, startPoint, endPoint, 0);
CGGradientRelease(gradient);
CGColorSpaceRelease(colorspace);
CGContextRestoreGState(currentContext);
UIImage *logoImage = [UIImage imageNamed:@"logo.png"];
[logoImage drawInRect:rect];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog (@"%@ was touched", self);
float red = (arc4random()%100)/100.0;
float green = (arc4random()%100)/100.0;
float blue = (arc4random()%100)/100.0;
UIColor *randomColor = [[UIColor alloc]initWithRed:red
green:green
blue:blue
alpha:1.0];
_circleColor = randomColor;
}
- (void)setCircleColor:(UIColor *)circleColor
{
_circleColor = circleColor;
[self setNeedsDisplay];
}
@end

浙公网安备 33010602011771号