小编Tra*_*ggs的帖子

iOS MapKit拖动注释(MKAnnotationView)不再平移地图

我正在学习在我初出茅庐的iOS应用程序中使用MapKit.我正在使用我的一些模型实体作为注释(将<MKAnnotation>协议添加到其头文件中).我还创建自定义MKAnnotationViews并将draggable属性设置为YES.

我的模型对象有一个location属性,它是一个CLLocation*.为了符合<MKAnnotation>协议,我在该对象中添加了以下内容:

- (CLLocationCoordinate2D) coordinate {
    return self.location.coordinate;
}

- (void) setCoordinate:(CLLocationCoordinate2D)newCoordinate {
    CLLocation* newLocation = [[CLLocation alloc]
        initWithCoordinate: newCoordinate
        altitude: self.location.altitude
        horizontalAccuracy: self.location.horizontalAccuracy
        verticalAccuracy: self.location.verticalAccuracy
        timestamp: nil];
    self.location = newLocation;
}

- (NSString*) title {
    return self.name;
}

- (NSString*) subtitle {
    return self.serialID;
}
Run Code Online (Sandbox Code Playgroud)

所以,我有4个必需的方法.而且他们非常直截了当.当我阅读苹果文档MKAnnotationView@draggable财产时,它说如下:

将此属性设置为YES会使用户可以拖动注释.如果是,则关联的注释对象还必须实现setCoordinate:方法.此属性的默认值为NO.

在其他地方,MKAnnotation文档说:

您对此属性的实现必须符合键值观察(KVO).有关如何实现对KVO的支持的更多信息,请参阅键值观察编程指南.

我已经读过那篇(简短的)文件了,我根本不清楚我应该做些什么来实现这一目标,以便coordinate我从我的location财产中得到的是一个适当的财产本身.

但我有理由相信它不能正常工作.当我拖动它时,它会移动,但是当我平移地图时它不再重新定位.

UPDATE

所以我试着玩MKPinAnnotationView股票.为此,我只是注释了我的委托mapView:viewForAnnotation:方法.我发现默认情况下这些都不可拖动.我添加了mapView:didAddAnnotationViews:我的委托以设置 …

properties key-value-observing mapkit ios ios7

3
推荐指数
1
解决办法
4328
查看次数

UIView重新出现时如何恢复动画?

我有一个自定义UIView子类(ValveStatusView).它是一个放置在自定义'UITableViewCell subclass (ValveCell) . Upon receivingawakeFromNib inValveStatusView`中的子视图,我设置了一些子图层,其中一个是动画的.动画设置代码如下所示:

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
animation.toValue = [NSValue valueWithCGPoint:CGPointMake(32, 64 + 6)];
    animation.duration = 0.75;
    animation.repeatCount = HUGE_VALF;
    animation.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseIn];
[drop addAnimation:animation forKey:@"position"];
Run Code Online (Sandbox Code Playgroud)

它使得"掉落"的图像永远地从水龙头反复掉落.这些单元格嵌入a UITableView,然后嵌入标签视图中.当我导航到另一个选项卡,然后返回时,我的动画不再运行了.我假设当包含我的视图的视图堆栈不再映射到窗口时,它会关闭动画.这是正确的假设吗?假设它是,当视图再次映射时,让它再次运行的惯用方法是什么?

我查看了这些UIView方法,但没有任何东西跳出来作为明显的"钩子"让它再次运行.我承认要浏览它们,而不是详尽地阅读它们.

core-animation uiview ios ios7

2
推荐指数
1
解决办法
1222
查看次数

如何判断我的ViewController何时再次成为堆栈顶部?

当使用a时UINavigationController,当用户"深入潜水"(推动堆叠中的另一个控制器)时,我有机会处理

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
Run Code Online (Sandbox Code Playgroud)

但是我该如何处理相反的事情呢?当用户按下后退按钮,并且控制器再次成为顶级控制器时,我希望它可能会更新某些状态,因为堆栈上的控制器可能已经改变了我想在现在可见的控制器中反映的一些内容.

或者,通过模拟,当我使用模态segue来呈现新的控制器时,我会选择一种方法,当呈现的控制器退出时,该方法被称为展开segue.如何使用导航堆栈管理控制器?

(随意在这上面写一个更好的标题)

uinavigationcontroller ios segue ios7

2
推荐指数
1
解决办法
112
查看次数

从多个参数创建对象的类方法

在Pharo中我想创建一个类方法,它创建一个新的person对象,并在一个方法中设置一个名称和年龄(两个参数)

Object subclass: #Person
        instanceVariableNames: 'name age'
        classVariableNames: ''
        category: '...'  
Run Code Online (Sandbox Code Playgroud)

但是我无法访问类方法中的实例变量.

name: n : age: a
        "Class method that creates a Person Object and defines its name and age"

        | person1 |
        person1 := self new.
        person1 name := n. "Unable to compile past this point due to red syntax highlighting
        person1 age := a.
        ^person1.
Run Code Online (Sandbox Code Playgroud)

我的目标是能够致电:

aPerson := Person name: 'Pharo' age: '4'
Run Code Online (Sandbox Code Playgroud)

smalltalk pharo

2
推荐指数
1
解决办法
1539
查看次数

在Smalltalk中调用构造方法

我有一个制作汽车对象的课程.它有两个实例变量:Make和Color.我在工作区内调用此方法时出现问题(如下所示)

类方法-Constructor

make: aMake color: aColor
    "Creates a new car object, sets its instance variables by the arguments"
    |car|
    car := self new.
    car setMake: aMake setColor: aColor. "accessor method below"
    ^car
Run Code Online (Sandbox Code Playgroud)

存取方法

setMake: make setColor: color
    "sets the instance variables"
    Make := make.
    Color := color.
Run Code Online (Sandbox Code Playgroud)

工作区(调用代码)

|car|
car := Car make: 'toyota' color: 'red'
Run Code Online (Sandbox Code Playgroud)

调用此行时,我收到"消息未被理解".这是什么问题?

smalltalk pharo

2
推荐指数
1
解决办法
978
查看次数

用于平衡alloc/free的C Pattern/Idiom

我有一堆遵循简单模式的代码:

Thing *myThing = newThing(); // allocation happens here
...
thingFuncA(myThing);
... and other computations ...
thingFuncB(myThing);
...
thingFree(myThing);
return result;
Run Code Online (Sandbox Code Playgroud)

应用程序的thingFuncX()变化与其他计算的变化一样,但最终总是免费的模式始终是相同的.

我需要在这里使用原始C(低,而不是C++,它的花式范围分配),我在半限制处理器上运行裸机.

有没有办法(ab)使用CPreprocessor来捕获这种常见模式.我想要使​​用一个成语,这样我就可以放心,不会忘记自由.我想我也许可以用一个宏做一些事情while { } do ()(一个例子的答案在这种情况下会有所帮助).或者也许还有一些其他聪明的C技巧我忽略了?

c memory-management allocation bare-metal

2
推荐指数
1
解决办法
122
查看次数

CGContext 和 CGContextRef 在 Swift 中相同吗?这是如何运作的?

今天早上我正在移植一些ObjectiveC自定义UIView子类Swift。为了使其更加“面向对象”,我遵循了使用CGContext方法扩展的示例。例如

extension CGContext {
    func fillColor(color:UIColor) {
        CGContextSetFillColorWithColor(self, color.CGColor)
    }
}
Run Code Online (Sandbox Code Playgroud)

因为我正在将目标 C 风格的消息(例如-(void) drawOutline: (CGContextRef) cr {...})转换为 Swift 风格的消息,而没有太注意(例如func drawOutline(cr:CGContextRef) {...}),所以我一开始并没有意识到我正在传递CGContextRef参数,而是进行了扩展CGContext(而不是 Ref)。但它成功了!

更奇怪的是,当我将这些CGContextRef' 更改为CGContext' 时。它仍然有效。不仅编译,而且运行和绘制正确。为了好玩,我改成了extension CGContextRef. 但它仍然继续发挥作用。就好像 Swift 足够聪明,可以将它们归结为同一件事。是吗?或者这里发生了更微妙/更酷的事情?

cgcontext cgcontextref swift

2
推荐指数
1
解决办法
1296
查看次数

我可以将NSLog与Swift结构一起使用吗?

我希望能够使用NSLogSwift结构.我可以吗?我现在正在做的是:

extension CGRect {
    var description:String {
        return "\(self.left),\(self.top)_|\(self.width),\(self.height)"
    }
 }

NSLog("rect for something %@", self.someView.frame.description)
Run Code Online (Sandbox Code Playgroud)

我曾希望简单地添加descriptionvar CGRect就足够了,我不需要.descriptionNSLog()调用中明确添加.但是,当我尝试我得到

Argument type 'CGRect' does not conform to expected type 'CVarArgType'
Run Code Online (Sandbox Code Playgroud)

似乎结构数组有类似的问题(因为数组也是结构?)

struct nslog swift

2
推荐指数
1
解决办法
1420
查看次数

Swift 2.1 [UInt8] --utf8-&gt;字符串?

我知道Stack Overflow和其他地方都存在这样的问题。但是它似乎也发展了很多。

给定一个列表UInt8(基本上是一个swift字节数组),最简单/惯用的方法是将其隐式化为swift String

我对不使用NSData / NSString的方法特别感兴趣,因为如果Santa将Swift带入Linux的世界,那么无疑将没有NS库,而且我想知道如何做到这一点。迅速。

utf-8 swift swift2.1

2
推荐指数
1
解决办法
1201
查看次数

这个 _dispatch_xref_dispose 错误是什么意思?

这是我不久前问过的一个问题的下一章。我有这个简化的计时器,它的灵感来自于 Matt Neuburg 书中的 Timer 对象。

import Foundation
import XCPlayground

class Timer {
    private var queue = dispatch_queue_create("timer", nil)
    private var source: dispatch_source_t!
    var tick:()->() = {}
    var interval:NSTimeInterval = 1

    func start() {
        self.stop()
        self.source = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, self.queue)
        dispatch_source_set_timer(source, DISPATCH_TIME_NOW, UInt64(self.interval) * 1000000000, 0)
        dispatch_source_set_event_handler(source, self.tick)
        dispatch_resume(self.source)
    }

    func stop() {
        if self.source != nil {
            dispatch_suspend(self.source)
        }
    }

}

var timer = Timer()
timer.interval = 5
timer.tick = { print("now \(NSDate())") }
timer.start()

timer.stop()
timer …
Run Code Online (Sandbox Code Playgroud)

timer grand-central-dispatch swift

2
推荐指数
1
解决办法
1629
查看次数