我想创建一个自定义CGPoint子类来添加一些属性,但我收到此错误:
从非协议,非类型类型'CGPoint'继承
我不知道为什么这是不可能的.我的课很简单:
import UIKit
class IYAttachPoint: CGPoint {
var holder: String = "No holder"
var unavailable: Bool = false
}
Run Code Online (Sandbox Code Playgroud)
我试过添加像CoreGraphics或QuartzCore这样的库没有成功.如果已经有一些问题或解决方案,请指出我正确的方向.
我需要绘制一条圆形线,并使用Core Animation用渐变填充此线.这不是一个主要问题,因为我正在使用a CAGradientLayer并使用a 屏蔽它CAShapeLayer.问题是渐变应该像在这个线程中看到的那样.
已经有很多解决方案,但让我解释为什么它们不适合我:
- 我无法使用图像因为我需要设置动画的位置属性数组 CAGradientLayer
- 我没有使用Core Graphics来绘制所有内容,因为正如我所说,我想要绘制的这个圆圈中的几乎所有内容都应该是可动画的.
- 这里提供的类是一个可能的解决方案,但不是最优的,因为它需要为我需要绘制的每个圆圈提供一个新视图,这对我来说不是最佳的(如果没有其他解决方案,我可能会使用这个)
- 我不能在这个问题中使用解决方案,因为它只适用于简单的双色渐变
这是我用来绘制圆圈的代码:
int radius = 100;
CAShapeLayer *circle = [CAShapeLayer layer];
circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(50, 60, 2.0*radius, 2.0*radius)cornerRadius:radius].CGPath;
circle.lineWidth = 10;
circle.fillColor = [UIColor clearColor].CGColor;
circle.strokeColor = [UIColor blackColor].CGColor;
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = CGRectMake(50, 60, 300, 300);
gradientLayer.startPoint = CGPointMake(0.5,1.0);
gradientLayer.endPoint = CGPointMake(0.5,0.0);
gradientLayer.frame = CGRectMake(0, 0, 300, 300);
NSMutableArray *colors = [NSMutableArray array];
NSMutableArray …Run Code Online (Sandbox Code Playgroud) 我知道Swift和Xcode 6都处于测试阶段,但我认为在我的情况下,存在一个与Swift或Xcode 6无关的结构性错误.如果stackoverflow社区将此评为不合适的问题,我可以立即将其删除.
但现在让我们回答我的问题.我有一个UIViewController,我正在尝试将相机胶卷中的最后一个图像添加到此视图控制器(显然是通过UIImageView).这是我的viewDidLoad方法:
override func viewDidLoad() {
super.viewDidLoad()
var assetLib = ALAssetsLibrary()
var url: NSURL = NSURL()
var imageView = UIImageView(frame: self.view.bounds)
assetLib.enumerateGroupsWithTypes(ALAssetsGroupType(ALAssetsGroupSavedPhotos), usingBlock: {
(group: ALAssetsGroup!, stop: CMutablePointer<ObjCBool>) in
group.setAssetsFilter(ALAssetsFilter.allPhotos())
group.enumerateAssetsAtIndexes(NSIndexSet(index: group.numberOfAssets()-1), options: nil, usingBlock: {
(result: ALAsset!, index: Int, stop: CMutablePointer<ObjCBool>) in
if result {
var alAssetRapresentation: ALAssetRepresentation = result.defaultRepresentation()
url = alAssetRapresentation.url()
if group == nil {
assetLib.assetForURL(url, resultBlock: {
(asset: ALAsset!) in
var assetRep: ALAssetRepresentation = asset.defaultRepresentation()
var iref = assetRep.fullResolutionImage().takeUnretainedValue()
var image = UIImage(CGImage: …Run Code Online (Sandbox Code Playgroud) 今天我将Xcode 6升级到beta 5(来自beta 1),你可以想象我发现我之前完美运行的Swift应用程序充满了各种错误(好吧,从beta 1有很多变化).在所有错误中,有一个我无法弄清楚如何修复.它与swift闭包有关,特别是.enumerateGroupsWithTypes方法的enumerationBlock参数.这是代码:
assetLib.enumerateGroupsWithTypes(ALAssetsGroupType(ALAssetsGroupSavedPhotos), usingBlock: {
(group: ALAssetsGroup?, stop: CMutablePointer<ObjCBool>) in
...
}, failureBlock: {
(error: NSError!) in
...
})
Run Code Online (Sandbox Code Playgroud)
这确实在Swift(Xcode 6 beta 1)中完美运行.但现在,我得到2个错误:
"'UnsafeMutablePointer'不是'错误类型'的子类型"
"使用未声明类型'CMutablePointer'"
很明显,CMutablePointer不再存在,所以我试图修改stop参数,如:
..., stop: UnsafeMutablePointer<ObjCBool> ...
Run Code Online (Sandbox Code Playgroud)
在这个改变之后,第二个错误显然消失了,但第一个转变为:
"无法找到接受所提供参数的'init'的重载"
我甚至尝试将UnsafeMutablePointer更改为UnsafePointer,如本文所述.
编辑:
以下是enumerateGroupsWithTypes方法的完整代码:
assetLib.enumerateGroupsWithTypes(ALAssetsGroupType(ALAssetsGroupSavedPhotos), usingBlock: {
(group: ALAssetsGroup?, stop: UnsafeMutablePointer<ObjCBool>) in
if group != nil {
group!.setAssetsFilter(ALAssetsFilter.allPhotos())
group!.enumerateAssetsAtIndexes(NSIndexSet(index: group!.numberOfAssets()-1), options: nil, usingBlock: {
(result: ALAsset!, index: Int, stop: UnsafeMutablePointer<ObjCBool>) in
if result {
var alAssetRapresentation: ALAssetRepresentation = result.defaultRepresentation()
url = alAssetRapresentation.url()
}
}) …Run Code Online (Sandbox Code Playgroud) 我最近发现了收藏视图,现在我的生活更加简单,但我有一个问题.我(显然)在视图中有一些单元格,但内部的内容可以被挤压.我使用一个在单元格框架外扩展的动画,正如您可以想象的那样,当单元格内部的圆圈变大时,附近的其他单元格会覆盖它.在我使用bringSubviewToFront之前,一切正常.但现在我很困惑,不知道如何"带来CellToFront".那我该怎么做呢?谢谢你的建议!
我正在将我的应用程序重写为Swift,到目前为止一切正常.但最近我试图翻译UICollectionViewFlowLayout子类,我遇到了一些问题.我无法理解如何翻译此类的layoutAttributesForElementsInRect方法...我只是得到了太多错误.这个课程的问题在于它是我没写过的单一课程.我从互联网上下载了这个类,所以我不明白它背后的逻辑.谁能帮我?这是完整的类,但我只需要layoutAttributesForElementsInRect方法,因为我已经成功翻译了第二个(layoutAttributesForItemAtIndexPath).哦,是的!布局基本上左对齐相应集合视图中的每个单元格!谢谢你的建议!
#import "IYAlignLeftLayout.h"
const NSInteger kMaxCellSpacing = 30;
@implementation IYAlignLeftLayout
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
NSArray* attributesToReturn = [super layoutAttributesForElementsInRect:rect];
for (UICollectionViewLayoutAttributes* attributes in attributesToReturn) {
if (nil == attributes.representedElementKind) {
NSIndexPath* indexPath = attributes.indexPath;
attributes.frame = [self layoutAttributesForItemAtIndexPath:indexPath].frame;
}
}
return attributesToReturn;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewLayoutAttributes* currentItemAttributes =
[super layoutAttributesForItemAtIndexPath:indexPath];
UIEdgeInsets sectionInset = UIEdgeInsetsMake(50, 20, 50, 20);
if (indexPath.item == 0) { // first item of section
CGRect frame = currentItemAttributes.frame;
frame.origin.x = sectionInset.left; // first item …Run Code Online (Sandbox Code Playgroud)