我需要一些帮助来填补我的道路.我不知道在哪里CGContextFillPath(path);
举例.我是绘图(和iOS开发)的新手,我现在已经尝试了近两个小时了.但是我达到了我必须放弃的程度......希望你能解释一下我的问题.我收到以下错误:invalid context
-(CGMutablePathRef) drawHexagon:(CGPoint)origin
{
//create mutable path
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, nil, origin.x, origin.y);
CGPoint newloc = CGPointMake(origin.x - 20, origin.y + 42);
CGPathMoveToPoint(path, NULL, newloc.x, newloc.y);
CGPathAddLineToPoint(path, NULL, newloc.x + 16,newloc.y + 38);
CGPathAddLineToPoint(path, NULL, newloc.x + 49, newloc.y + 0);
CGPathAddLineToPoint(path, NULL, newloc.x + 23, newloc.y - 39);
CGPathAddLineToPoint(path, NULL, newloc.x - 25,newloc.y - 40);
CGPathAddLineToPoint(path, NULL, newloc.x -43, newloc.y + 0);
CGPathCloseSubpath(path);
CGContextAddPath(context, path);
context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor …
Run Code Online (Sandbox Code Playgroud) 我有三个点,x和y值开头.我真正想要的是实际矢量的位置(看图像提供).你能帮助我吗 ?我尝试了atan2和平行四边形,但不幸的是没有成功.
我有代码重复X-和Y-这是:
bg = [CCSprite spriteWithFile:@"ipadbgpattern.png" rect:CGRectMake(0, 0, 3000, 3000)];
bg.position = ccp(500,500);
ccTexParams params = {GL_LINEAR,GL_LINEAR,GL_REPEAT,GL_REPEAT};
[bg.texture setTexParameters:¶ms];
[self addChild:bg];
Run Code Online (Sandbox Code Playgroud)
但是,我不知道如何更改参数以使背景沿水平轴重复.
我正在使用自动参考计数.我有一个自定义UIViewController
子类,每当我-presentViewController: animated:completion:
从NSLog
superview 调用或删除它的视图时,我想要"我被释放"这样的东西,所以我知道视图控制器已成功删除.我-dealloc
在视图控制器中实现了该方法.然而,我开始了一个测试项目,其中我只有两个UIViewController
实例(没有保留周期),并且-dealloc
当我按模式推送第二个UIViewController
或当我删除超级视图或从父视图控制器中删除它时,都没有调用.我错过了什么吗?在我原来的项目中(不是测试用例),仪器向我展示了那些控制器留下了我无法摆脱的内存占用.
objective-c uiviewcontroller dealloc ios automatic-ref-counting
我正在尝试将iOS应用程序移植到Mac上,并且在转换过程中遇到了一些问题.其中之一是定制NSTableView
.究竟是什么区别NSCell
,NSTableRowView
和自定义NSView
基于NSTableview
?我最初开始时基于视图NSTableView
,但我很快注意到我必须自己处理选择.我无法将其拉下来,所以我继续使用NSTableRowView
,奇怪的是,它并没有调用我习惯的初始化器NSTableRowView
.
我基本上只想要一个具有自定义内容的自定义表格视图单元格,这是可选择的.最好的方法是什么?
在iOS上,我只是子类UITableViewCell
并设置其selectedView属性.在Mac上,这似乎比这更复杂.
我想遍历 Swift 中的视图控制器层次结构并找到一个特定的类。这是代码:
extension UIViewController{
func traverseAndFindClass<T : UIViewController>() -> UIViewController?{
var parentController = self.parentViewController as? T?
^
|
|
// Error: Could not find a user-defined conversion from type 'UIViewController?' to type 'UIViewController'
while(parentController != nil){
parentController = parentController!.parentViewController
}
return parentController
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我知道 parentViewController 属性返回一个可选的 UIViewController,但我不知道如何以上帝的名义使 Generic 成为可选类型。也许使用某种 where 子句?
我正在尝试实现一个允许我存储一组弱观察者的结构.
这是观察者包装器:
public func ==<T: Hashable>(lhs: WeakObserver<T>, rhs: WeakObserver<T>) -> Bool {
return lhs.hashValue == rhs.hashValue
}
public struct WeakObserver<T where T: AnyObject, T: Hashable> : Hashable {
private weak var weakObserver : T?
public init(weakObserver: T){
self.weakObserver = weakObserver
}
public var hashValue : Int {
return self.weakObserver!.hashValue
}
}
Run Code Online (Sandbox Code Playgroud)
这是每个观察者需要遵守的协议:
public protocol DataModelObserverProtocol : class, Hashable, AnyObject {
func someFunc()
}
Run Code Online (Sandbox Code Playgroud)
用法:
public class DataModel: NSObject, DataModelInterface {
public var observers = Set<WeakObserver<DataModelObserverProtocol>>()
//^^^^^ Using 'DataModelObserverProtocol' as a …
Run Code Online (Sandbox Code Playgroud) 我在屏幕上有9个六角形精灵,我必须在它们周围指定一条特定的路径才能使它们的区域可触摸.我不想分别为每条路径设置坐标,所以我不能只使用第一条路径(六边形大小都相同)并将其原点移动到另一个位置(不破坏形状)?(如果我现在这样做,CGPathAddLineToPoint();会从前六边形中添加六边形的点.我希望我能得到这个想法......(见图片......注意:右上方的灰色八边形是与黑色完全相同的尺寸和形状)![移动路径坐标和形状] [1]
hex2TouchArea = CGPathCreateMutable();
CGPathMoveToPoint(hex2TouchArea, NULL, 150,157);
CGPathAddLineToPoint(hex2TouchArea, NULL, 130, 198);
CGPathAddLineToPoint(hex2TouchArea, NULL, 146, 236);
CGPathAddLineToPoint(hex2TouchArea, NULL, 195, 236);
CGPathAddLineToPoint(hex2TouchArea, NULL, 218, 197);
CGPathAddLineToPoint(hex2TouchArea, NULL, 193, 157);
CGPathCloseSubpath(hex2TouchArea);
Run Code Online (Sandbox Code Playgroud)
在这里我把图片显示在图像中
http://666kb.com/i/bz2ysevuw8n65rh3i.gif
*编辑:我从帖子中得到了解决方案并稍微改了一下:*
-(CGMutablePathRef) drawHexagon:(CGPoint)origin
{
//create mutable path
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, nil, origin.x, origin.y);
CGPoint newloc = CGPointMake(origin.x - 20, origin.y + 42);
CGPathMoveToPoint(path, NULL, newloc.x, newloc.y);
CGPathAddLineToPoint(path, NULL, newloc.x + 16,newloc.y + 38);
CGPathAddLineToPoint(path, NULL, newloc.x + 49, newloc.y + 0);
CGPathAddLineToPoint(path, NULL, newloc.x + 23, …
Run Code Online (Sandbox Code Playgroud) 我在我的故事板中有一个视图,我分配了一个名为"MainView"的标识符.但是,如果我将其视图添加到子视图,则后面的所有内容都会导致崩溃(例如,按下按钮)
MainViewController *mvc = [self.storyboard instantiateViewControllerWithIdentifier:@"MainView"];
[self.view addSubview:mvc.view];
Run Code Online (Sandbox Code Playgroud)
这是按钮触发的动作:(MainViewController.h)
-(IBAction)showUsername:(id)sender{
[testLabel setText:@"username"];
}
Run Code Online (Sandbox Code Playgroud)
和崩溃日志:
-[MainViewController performSelector:withObject:withObject:]: message sent to deallocated instance 0x44e0810
Run Code Online (Sandbox Code Playgroud)
我用ARC.
iphone objective-c storyboard uiviewcontroller automatic-ref-counting
我有一个符合接口的对象数组
interface SomeInterface{}
Run Code Online (Sandbox Code Playgroud)
和一个符合该接口的类
class SomeClass : NSObject, SomeInterface{}
Run Code Online (Sandbox Code Playgroud)
现在,我创建一个SomeInterface对象数组
var myInterfaceObjects : SomeInterface[] = SomeInterface[]()
Run Code Online (Sandbox Code Playgroud)
然后我想将此数组转换为SomeClass对象数组
var someClassObjects : SomeClass[] = myInterfaceObjects as SomeClass[] //CRASH!
Run Code Online (Sandbox Code Playgroud)
我如何在没有崩溃的情况下垂头丧气?
fatal error: can't reinterpretCast values of different sizes
(lldb) bt
* thread #1: tid = 0x935ef, 0x001986b8 libswift_stdlib_core.dylib`Swift.reinterpretCast <A, B>(A) -> B + 220, queue = 'com.apple.main-thread', stop reason = EXC_BREAKPOINT (code=EXC_ARM_BREAKPOINT, subcode=0xe7ffdefe)
* frame #0: 0x001986b8 libswift_stdlib_core.dylib`Swift.reinterpretCast <A, B>(A) -> B + 220
frame #1: 0x001bebdc libswift_stdlib_core.dylib`Swift.ContiguousArrayBuffer.storesOnlyElementsOfType <A>(Swift.ContiguousArrayBuffer<A>)<B>(B.Type) -> Swift.Bool + 912 …
Run Code Online (Sandbox Code Playgroud) objective-c ×6
ios ×3
swift ×3
cgpath ×2
generics ×2
iphone ×2
arrays ×1
dealloc ×1
ipad ×1
macos ×1
nstableview ×1
position ×1
storyboard ×1
textures ×1
vector ×1