在应用程序中,我绘制了一个弯曲的UIBezierPath和一个MKOverlayPathView类来显示航线.这是我正在使用的代码:
Run Code Online (Sandbox Code Playgroud)- (UIBezierPath *)pathForOverlayForMapRect:(MKMapRect)mapRect { ... bla bla bla ... UIBezierPath *path = [UIBezierPath bezierPath]; [path moveToPoint:s]; [path addQuadCurveToPoint:e controlPoint:cp1]; [path addLineToPoint:e2]; [path addQuadCurveToPoint:s2 controlPoint:cp2]; [path closePath]; return path; }
- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context{
self.mapRect = mapRect;
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);
CGContextSetLineWidth(context, mapRect.size.height/700);
CGContextSetLineJoin(context, kCGLineJoinRound);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextAddPath(context, [self pathForOverlayForMapRect:mapRect].CGPath);
[self updateTouchablePathForMapRect:mapRect];
CGContextDrawPath(context, kCGPathFillStroke);
}
Run Code Online (Sandbox Code Playgroud)
这工作得很好,但我想在该路径上绘制一个渐变,而不仅仅是填充颜色.这就是它开始变得非常棘手的地方.
我已经尝试过CGContextDrawLinearGradient(),但它还没有让我有用.
UIScene当连接多个场景时,我无法获取电流。
具体来说,我的应用程序(iOS 13)支持 iPad 上的多个窗口。当我有两个并排的窗口并访问应用程序连接的场景时(使用UIApplication.shared.connectedScenes),它们都activationState设置为.foregroundActive. 但我实际上只与其中之一互动。
我在 stackoverflow 上找到了一些代码来检索当前的关键窗口(不记得是谁发布的),但正如您所见,它总是会返回两个窗口之一。
let keyWindow: UIWindow? = UIApplication.shared.connectedScenes
.filter({$0.activationState == .foregroundActive})
.map({$0 as? UIWindowScene})
.compactMap({$0})
.first?.windows
.filter({$0.isKeyWindow}).first
Run Code Online (Sandbox Code Playgroud)
我是否缺少某些方法来确定与之交互的最后一个窗口,或者多个.foregroundActive状态是否是错误?
谢谢!
我想计算一个矩形的最小边界,以满足一个特定字体的字符串(多行).
它应该看起来像这样:
FROM:
----------------------------------
|Sent when the application is about|
|to move from active to inactive |
|state. |
----------------------------------
TO:
-------------------------
|Sent when the application|
|is about to move from |
|active to inactive state.|
-------------------------
Run Code Online (Sandbox Code Playgroud)
如您所见,高度保持不变,宽度变为必要的最小值.
最初我虽然我可以使用boundingRectWithSize(约束到最大宽度)首先获得所需的最小高度,然后调用boundingRectWithSize(约束到计算的高度)得到宽度.但是在第二步计算宽度时会产生错误的结果.它不考虑最大高度,而是简单计算单个线串的宽度.
之后我找到了一种方法来获得正确的结果,但执行此代码需要很长时间,这对我没用:
首先计算约束宽度所需的rect:
var objectFrame = Class.sizeOfString(string, font: objectFont, width: Double(width), height: DBL_MAX)
Run Code Online (Sandbox Code Playgroud)
那么宽度:
objectFrame.size.width = Class.minWidthForHeight(string, font: objectFont, objectFrame.size.height)
Run Code Online (Sandbox Code Playgroud)
使用:
class func minWidthForHeight(string: NSString, font: UIFont, height: CGFloat) -> CGFloat
{
let deltaWidth: CGFloat = 5.0
let …Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我有一个带有几个 MKGeodesicPolylines 的 MapView。我希望能够识别这些线上的触摸手势。使用以下方法添加叠加层:
[_mapView addOverlay:polyline];
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id < MKOverlay >)overlay
{
if ([overlay class] == [MKGeodesicPolyline class])
{
MKPolylineRenderer *renderer = [[[MKPolylineRenderer alloc] initWithPolyline:overlay] autorelease];
renderer.lineWidth = 4.0;
renderer.strokeColor = [UIColor blackColor];
return renderer;
}
return nil;
}
Run Code Online (Sandbox Code Playgroud)
我试过WildcardGestureRecognizer应该符合我的目的。这是我正在使用的代码:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
.... MapView init ....
WildcardGestureRecognizer * tapInterceptor = [[WildcardGestureRecognizer alloc] init];
tapInterceptor.touchesBeganCallback = ^(NSSet * touches, UIEvent * event) {
UITouch *touch = [touches anyObject]; …Run Code Online (Sandbox Code Playgroud) 我想绘制一个圆形渐变,如下面的示例图片所示。我可以轻松管理径向渐变,但我不确定如何制作圆形渐变。我想在一条线上画一个渐变,然后把它转换成一个圆圈。那可能吗?

这就是我绘制径向渐变的方式:
CGFloat a = MIN(self.frame.size.width, self.frame.size.height) - _lineWidth;
//Get current context
CGContextRef mainContext = UIGraphicsGetCurrentContext();
CGRect newRect = rect;
newRect.origin.x = ((self.frame.size.width - a)/2.0f);
newRect.origin.y = ((self.frame.size.height - a)/2.0f);
newRect.size.width = a;
newRect.size.height = a;
// Fill circle
const CGFloat *_components = CGColorGetComponents(_fillColor.CGColor);
CGFloat red = _components[0];
CGFloat green = _components[1];
CGFloat blue = _components[2];
CGFloat alpha = _components[3];
CGColorSpaceRef baseSpace = CGColorSpaceCreateDeviceRGB();
CGFloat redBallColors[] = {
red,green,blue,0.5,
red,green,blue,alpha,
};
CGFloat glossLocations[] = {0.0, 1.0};
CGGradientRef ballGradient = CGGradientCreateWithColorComponents(baseSpace, …Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我想呈现一个透明的UIView(或图层),它充当滤色器.因此,当此视图显示在下面的所有内容时,它将以灰度显示.
这是可能的,如果是的话你会怎么做呢?我知道如何专门用灰度渲染UIView,但这不是我想要实现的.
谢谢!
ios ×4
gradient ×2
swift ×2
calayer ×1
cgsize ×1
ipad ×1
mkpolyline ×1
nsstring ×1
objective-c ×1
swift5.1 ×1
uibezierpath ×1
uiscene ×1
uiview ×1
xcode ×1
xcode11 ×1