- 创建支架视图。
- 创建两个形状:蓝色框和灰色框。
- 将用于两个框的路径组合起来,创建第三个形状,用于遮盖支架视图。
UIView * background = [UIView new];
background.frame = self.view.bounds;
background.backgroundColor = [self colourForHex:@"#F5F5F5" andAlpha:1.0f];
[self.view addSubview:background];
UIView * holder = [UIView new];
holder.frame = CGRectMake((w-300)/2, (h-300)/2, 300, 300);
[background addSubview:holder];
UIBezierPath * boxPath = [UIBezierPath bezierPath];
[boxPath moveToPoint:CGPointMake(0, 0)];
[boxPath addLineToPoint:CGPointMake(0, 100)];
[boxPath addLineToPoint:CGPointMake(200, 100)];
[boxPath addLineToPoint:CGPointMake(200, 0)];
[boxPath closePath];
UIBezierPath * castPath = [UIBezierPath bezierPath];
[castPath moveToPoint:CGPointMake(10, 100)];
[castPath addLineToPoint:CGPointMake(0, 200)];
[castPath addLineToPoint:CGPointMake(200, 200)];
[castPath addLineToPoint:CGPointMake(190, 100)];
[castPath closePath];
CAShapeLayer * boxShape = [CAShapeLayer layer];
boxShape.path = boxPath.CGPath;
boxShape.fillColor = [self colourForHex:@"#1A0F53" andAlpha:1.0f].CGColor;
boxShape.shadowColor = [UIColor blackColor].CGColor;
boxShape.shadowOffset = CGSizeMake(5, 5);
boxShape.shadowRadius = 2.0f;
boxShape.shadowOpacity = 0.5f;
[holder.layer addSublayer:boxShape];
CAShapeLayer * castShape = [CAShapeLayer layer];
castShape.path = castPath.CGPath;
castShape.fillColor = [self colourForHex:@"#D8D8D8" andAlpha:1.0f].CGColor;
[holder.layer insertSublayer:castShape below:boxShape];
UIBezierPath * maskPath = [UIBezierPath bezierPath];
[maskPath appendPath:boxPath];
[maskPath appendPath:castPath];
CAShapeLayer * maskShape = [CAShapeLayer layer];
maskShape.path = maskPath.CGPath;
[holder.layer setMask:maskShape];
根据评论编辑:要添加 gif 中显示的动画,您可以将 UIPanGestureRecogniser 添加到视图并在用户滑动时偏移(原始要求),或者,在上面,我添加了一些类似这样的代码:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[UIView animateWithDuration:0.5f
delay:0.0f
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
boxShape.transform = CATransform3DMakeTranslation(100, 0, 0);
}
completion:^(BOOL finished){
}];
});