小编Gui*_*ume的帖子

如何使用Cocoa Auto Layout窗口调整自定义视图的大小?

我有一个窗口,其中包含一个自定义视图,我希望自定义视图可以随窗口调整大小,以便随时完全填充它.如果我写:

NSView *contentView = [self.window contentView];
CustomView *customView = [[CustomView alloc] initWithFrame:[contentView bounds]];
[contentView addSubview:customView];
[contentView addConstraint:
    [NSLayoutConstraint constraintWithItem:customView
        attribute:NSLayoutAttributeWidth
        relatedBy:NSLayoutRelationEqual
        toItem:contentView
        attribute:NSLayoutAttributeWidth
        multiplier:1
        constant:0]];
[contentView addConstraint:
    [NSLayoutConstraint constraintWithItem:customView
        attribute:NSLayoutAttributeHeight
        relatedBy:NSLayoutRelationEqual
        toItem:contentView
        attribute:NSLayoutAttributeHeight
        multiplier:1
        constant:0]];
Run Code Online (Sandbox Code Playgroud)

然后窗口不允许我调整它.
如果我添加:

[customView setTranslatesAutoresizingMaskIntoConstraints:NO];
Run Code Online (Sandbox Code Playgroud)

然后自定义视图不会出现(drawRect:似乎永远不会被调用).我尝试了不同的方式(包括视觉格式@"|[customview]|"),但我总是遇到同样的问题.我知道可以使用旧的自动调整系统来完成,包括:

[customView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
Run Code Online (Sandbox Code Playgroud)

但是我想使用Cocoa Auto Layout系统,我想将它用于更复杂的情况(比如总是填满窗口的几个自定义视图).

有谁知道什么是错的,我应该如何使用自动布局系统来获得我想要的结果?

layout cocoa

71
推荐指数
3
解决办法
5万
查看次数

如何继承CALayer并使用自定义属性?

我正在尝试CALayer使用自定义index属性创建一个子类,我可以直接设置动画和更改,以便根据索引显示不同的图片.

在标题中,我声明:

@property NSUInteger index;
Run Code Online (Sandbox Code Playgroud)

在实施中,我覆盖了needDisplayForKey:

+ (BOOL)needsDisplayForKey:(NSString *)key
{
    if ([key isEqualToString:@"index"])
        return YES;
    else
        return [super needsDisplayForKey:key];
}
Run Code Online (Sandbox Code Playgroud)

现在,在该display方法中,我想基于索引显示图片.但是,在动画期间,值self.index永远不会更改,因此我必须查询表示层的值,这与通过子类提供CALayer内容的示例相反:

- (void)display
{
    NSUInteger presentationIndex = [(CustomLayer *)[self presentationLayer] index];
    self.contents = (id)[[images objectAtIndex:presentationIndex] CGImage];
}
Run Code Online (Sandbox Code Playgroud)

问题是,如果我这样做,我不能index直接在动画之外设置值,因为它只会更改模型层,并且该display方法显式查询表示层.

如果我添加一个复制值的初始化方法index,它的工作原理如下:

- (id)initWithLayer:(id)layer
{
    self = [super initWithLayer:layer];
    if (self) {
        if ([layer isKindOfClass:[CustomLayer class]])
            self.index = [(CustomLayer *)layer index];
    }
    return self;
} …
Run Code Online (Sandbox Code Playgroud)

iphone core-animation calayer

15
推荐指数
1
解决办法
1万
查看次数

如何使NSStringDrawingContext缩小文本?

我正在尝试使用iOS 6的属性字符串API来计算文本的大小,并在必要时缩小字体大小.但是,正如文档所说,我无法让它工作.

NSString *string = @"This is a long text that doesn't shrink as it should";

NSStringDrawingContext *context = [NSStringDrawingContext new];
context.minimumScaleFactor = 0.5;

UIFont *font = [UIFont fontWithName:@"SourceSansPro-Bold" size:32.f];

NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.lineBreakMode = NSLineBreakByClipping;

NSDictionary *attributes = @{ NSFontAttributeName: font,
                              NSParagraphStyleAttributeName: paragraphStyle };

NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:self.title attributes:attributes];

CGRect rect = [attributedString boundingRectWithSize:CGSizeMake(512.f, 512.f) options:NSStringDrawingUsesLineFragmentOrigin context:context];

NSLog(@"rect: %@, context: %@", NSStringFromCGRect(rect), context.debugDescription);
Run Code Online (Sandbox Code Playgroud)

但是文本没有缩小并被截断.actualScaleFactor永远1.日志结果如下:

rect:{{0, 0}, {431.64801, 80.447998}}, context:<NSStringDrawingContext: 0x14e85770> minimumScaleFactor:0.500000 …
Run Code Online (Sandbox Code Playgroud)

text nsattributedstring ios

14
推荐指数
2
解决办法
5617
查看次数

Step Over Thread和Step Into Thread命令有什么用?

有人可以解释Xcode中Step Over ThreadStep Into Thread调试器命令的目的是什么?在什么情况下使用它们而不是通常的Step OverStep Into是有用的?有什么区别,什么时候重要?

编辑:为了澄清这个问题,我不是在询问Step Over/Step Into/Step Out之间的区别,我问的是普通版和"Thread"版本之间的区别,在什么情况下一个版本更多比另一个有用.

debugging xcode gdb lldb

8
推荐指数
3
解决办法
5723
查看次数

在部分之间重新排列项目时,UICollectionView会崩溃

我在a之间的部分之间制作动画时遇到了麻烦UICollectionView,我的程序一直在崩溃,它有什么问题?

我有一个集合视图,它有四个部分:

0:A
1:B
2:C
3:D

我想将它转换为只有三个具有相同项目的部分:

0:A
1:B,C
2:D

我想为这种转变制作动画:

// Initial state

NSArray *source = @[ @[@"A"], @[@"B"], @[@"C"], @[@"D"] ];


// Some data source methods

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [source[section] count];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return [source count];
}


// Transformation: that works but I have to keep an empty section

source = @[ @[@"A"], @[@"B", @"C"], @[@"D"], @[] ];

[collection performBatchUpdates:^{
    [collection moveItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]
                        toIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
    [collection moveItemAtIndexPath:[NSIndexPath indexPathForItem:0 …
Run Code Online (Sandbox Code Playgroud)

crash ios uicollectionview

5
推荐指数
1
解决办法
2719
查看次数

如何在自定义容器视图控制器中实现交互式转换

我实现了自己的自定义容器视图控制器,并尝试使其与iOS 7视图控制器转换兼容.我让我的自定义容器视图控制器符合UIViewControllerContextTransitioning我送self当我打电话transitionDuration:animateTransition:.只要我只使用动画过渡,一切正常.

现在我想让它与交互式转换一起工作,所以我再次使用交互控制器startInteractiveTransition:代替动画控制器animateTransition:,self再次使用它作为参数.但是,如果我使用a UIPercentDrivenInteractiveTransition作为交互控制器,它会_animator在我的上下文中调用一个方法(这是容器视图控制器本身).当然,我没有实现私有和无证的这种方法,所以它崩溃了......

我在实施中遗漏了什么吗?是UIPercentDrivenInteractiveTransition唯一与苹果类兼容,因为它使用了一些魔法执行(当它要求一切都应该是在为UIView动画块)?文档和头文件使我们看起来像我们可以实现自己的容器视图控制器并仍然使用自定义转换,但这是真的还是只是一厢情愿,因为没有人真的会这样做?

如果我不能使用UIPercentDrivenInteractiveTransition,那么交互/动画逻辑究竟应该在哪里?在UIViewControllerTransitionCoordinatorContext对象?在UIViewControllerInteractiveTransitioning对象中(很可能,这个对象是驱动程序......)?或者在UIViewControllerAnimatedTransitioning对象中(这可能是真实动画应该发生的地方,但这意味着animateTransition:在交互过程中多次调用?或者为交互过渡的每个步骤添加新方法?)

编辑:文档说:

百分比驱动的交互式转换对象在一个视图控制器的消失和另一个视图控制器的外观之间驱动自定义动画.它依赖于过渡动画师委托 - 一个采用UIViewControllerAnimatorTransitioning协议的自定义对象- 来设置和执行动画.

没有UIViewControllerAnimatorTransitioning协议.假设它是一个错误,或者在iOS 7开发过程中发生的名称更改,它实际上是UIViewControllerAnimatedTransitioning协议,我们如何将交互控制器与动画控制器链接?我想这是驱动转换的视图控制器的责任,但我没有看到任何API来建立这个链接,所以这意味着它UIPercentDrivenInteractiveTransition确实是为Apple类保留的?

containers transition interactive uiviewcontroller ios

5
推荐指数
1
解决办法
2521
查看次数

如何管理在辅助线程中运行的NSRunLoop的自动释放池?

在Apple的MVCNetworking示例代码中,NetworkManager该类包含此方法以在专用于网络活动的辅助线程中维护运行循环(以便NSURLConnection异步运行):

- (void)networkRunLoopThreadEntry
{
    while(YES) {
        NSAutoreleasePool *pool;
        pool = [[NSAutorelease alloc] init];
        [[NSRunLoop currentRunLoop] run];
        [pool drain];
    }
}
Run Code Online (Sandbox Code Playgroud)

因为run如果没有连接到运行循环源方法立即退出,这看起来像一个无限while这是会无谓地消耗CPU资源,如果目前没有连接到运行循环NSURLConnection的循环.

另一方面,为了保持运行循环活动,一些人 建议在运行循环中安排一个空端口:

 - (void)networkRunLoopThreadEntry
 {
     NSAutoreleasePool *pool = [[NSAutorelease alloc] init];
     NSPort *port = [NSPort port];
     [[NSRunLoop currentRunLoop] addPort:port forMode:NSRunLoopCommonModes];
     [NSRunLoop run];
     [pool drain];
 }
Run Code Online (Sandbox Code Playgroud)

但是,在这种情况下,我担心该run方法永远不会退出,这意味着池永远不会被耗尽,这意味着在辅助线程中分配和自动释放的所有对象都将泄漏.

那是什么方式?

(对于上下文,和其他许多人一样,我正在尝试将异步封装NSURLConnection在a中NSOperation,这意味着它可以在主线程之外触发.此外,MVCNetworking示例代码,以及WWDC 2010会话Network Apps for iPhone操作系统,似乎建议有一个专用于网络传输的独特辅助线程以防止主线程上的延迟.)

cocoa objective-c nsrunloop

4
推荐指数
1
解决办法
2426
查看次数

强制按比例/转换重绘UIView或CALayer

假设您有一个UIViewCALayer自己绘制(矢量)绘图,并且希望它保持任何大小的清晰,即使缩放视图或图层时也是如此。随transform属性缩放时如何重绘?是强制bounds更改而不使用的唯一方法transform吗?还是setNeedsDisplay每个步骤都打电话?

为什么UIImageView似乎表现出不同的行为(即,即使使用缩放transform并且其bounds不变,它似乎总是适当地缩放其源图像)?是因为这是直接设置content基础属性的唯一情况CALayer吗?

scale calayer uiview drawrect ios

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