我有六个按钮的Objective-C工作代码,但我似乎没有找到如何使用Swift更改按钮的状态.那么如何让以下代码在Swift中运行?
[self.button2 setBackgroundImage:[UIImage imageNamed:@"button2_selected.png"] forState: UIControlStateSelected];
button1.selected = NO;
button2.selected = !button2.selected;
button3.selected = NO;
button4.selected = NO;
button5.selected = NO;
button6.selected = NO;
Run Code Online (Sandbox Code Playgroud) 我试过这个通过使用uicollectionview的扩展来获取集合视图中心的单元格的索引,但我总是得到nil而不是索引.我怎样才能解决这个问题?
extension UICollectionView {
var centerPoint : CGPoint {
get {
return CGPoint(x: self.center.x + self.contentOffset.x, y: self.center.y + self.contentOffset.y);
}
}
var centerCellIndexPath: NSIndexPath? {
if let centerIndexPath: NSIndexPath = self.indexPathForItemAtPoint(self.centerPoint) {
return centerIndexPath
}
return nil
}
}
Run Code Online (Sandbox Code Playgroud)
那么:在随机方法的UIViewController中我有这个:
if let centerCellIndexPath: NSIndexPath = collectionTemp!.centerCellIndexPath {
print(centerCellIndexPath)
} else {
println("nil")
}
Run Code Online (Sandbox Code Playgroud)
索引路径总是为零,我不明白为什么,因为单元格按顺序显示,除此之外一切都很好.
我很困惑.通常在iOS应用程序中,我将使用url方案访问设置.现在我发现使用Objective-C在MacOSX上你可以这样做:
NSDictionary *options = @{(id)kAXTrustedCheckOptionPrompt: @YES};
BOOL accessibilityEnabled = AXIsProcessTrustedWithOptions((CFDictionaryRef)options);
Run Code Online (Sandbox Code Playgroud)
我试图将其转换为Swift但没有得到有效的结果.有谁知道如何转换这个或一个有效的方法来请求在Swift中的辅助访问?
我有一些看起来像这样的Realm类:
class Friends: Object {
dynamic var name = true
dynamic var role = true
dynamic var type = true
dynamic var owner: Profile?
}
class Profile: Object {
dynamic var uuid = NSUUID().UUIDString
dynamic var name = ""
dynamic var date = NSDate(timeIntervalSinceNow: 1)
dynamic var section = 0
dynamic var code = ""
dynamic var gender = 0
dynamic var type = ""
let friends = List<Friends>()
override static func primaryKey() -> String? {
return "uuid"
}
} …Run Code Online (Sandbox Code Playgroud) 我想制作一个具有8个相等切片的漂亮图形饼图,可以根据一个Int或类似的东西进行单独缩放或调整大小。如下所示,只是将所有切片均等地切割:

我曾在Objective-C中尝试过此方法,但它仅占一小部分:
-(CAShapeLayer *)createPieSlice {
CAShapeLayer *slice = [CAShapeLayer layer];
slice.fillColor = [UIColor redColor].CGColor;
slice.strokeColor = [UIColor blackColor].CGColor;
slice.lineWidth = 3.0;
CGFloat angle = DEG2RAD(-60.0);
CGPoint center = CGPointMake(100.0, 100.0);
CGFloat radius = 100.0;
UIBezierPath *piePath = [UIBezierPath bezierPath];
[piePath moveToPoint:center];
[piePath addLineToPoint:CGPointMake(center.x + radius * cosf(angle), center.y + radius * sinf(angle))];
[piePath addArcWithCenter:center radius:radius startAngle:angle endAngle:DEG2RAD(60.0) clockwise:YES];
// [piePath addLineToPoint:center];
[piePath closePath]; // this will automatically add a straight line to the center
slice.path = piePath.CGPath;
return slice;
} …Run Code Online (Sandbox Code Playgroud) swift ×5
ios ×4
objective-c ×2
cocoa ×1
graphic ×1
macos ×1
nsindexpath ×1
object ×1
permissions ×1
quartz-core ×1
realm ×1
realm-list ×1
uibezierpath ×1
uibutton ×1
xcode ×1