作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我想使用-frameForAlignmentRect:
和-alignmentRectForFrame:
在我的ios应用程序中指定对齐矩形。但是这个方法没有调用。
我有几乎相同的iOS和Mac代码。它在OS X上运行得很好,但在iOS上却不行。
Mac版本
@implementation CustomView
#define SHADOW_SIZE 10
- (void)drawRect:(NSRect)dirtyRect {
NSBezierPath *path;
[[NSColor redColor] set];
path = [NSBezierPath bezierPathWithRect:dirtyRect];
[path fill];
// Draw a shadow
NSRect rect = NSMakeRect(SHADOW_SIZE, 0, dirtyRect.size.width - SHADOW_SIZE, dirtyRect.size.height - SHADOW_SIZE);
path = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:32 yRadius:32];
[[[NSColor blackColor] colorWithAlphaComponent:0.3f] set];
[path fill];
// Draw shape
rect.origin = CGPointMake(0, SHADOW_SIZE);
path = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:32 yRadius:32];
[[NSColor orangeColor] set];
[path fill];
}
#define USE_ALIGNMENT_RECTS 1
#if USE_ALIGNMENT_RECTS
- (NSRect)frameForAlignmentRect:(NSRect)alignmentRect {
NSRect rect = alignmentRect;
rect.origin.y -= SHADOW_SIZE;
rect.size.width += SHADOW_SIZE;
rect.size.height += SHADOW_SIZE;
return rect;
}
- (NSRect)alignmentRectForFrame:(NSRect)frame {
NSRect rect;
rect.origin = CGPointMake(frame.origin.x, frame.origin.y + SHADOW_SIZE);
rect.size.width = frame.size.width - SHADOW_SIZE;
rect.size.height = frame.size.height - SHADOW_SIZE;
return rect;
}
#endif
@end
@implementation AppDelegate
- (void)awakeFromNib {
_view = _window.contentView;
CustomView *view = [[CustomView alloc] init];
[self.view addSubview:view];
view.translatesAutoresizingMaskIntoConstraints = NO;
// Set vertical and horizontal alignments and view size
// A little trick from here https://github.com/evgenyneu/center-vfl
NSDictionary *views = NSDictionaryOfVariableBindings(_view, view);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[_view]-(<=1)-[view(200)]"
options:NSLayoutFormatAlignAllCenterY
metrics:nil
views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_view]-(<=1)-[view(200)]"
options:NSLayoutFormatAlignAllCenterX
metrics:nil
views:views]];
}
@end
@implementation MyView
#define SHADOW_SIZE 10
- (void)drawRect:(CGRect)dirtyRect {
UIBezierPath *path;
[[UIColor redColor] set];
path = [UIBezierPath bezierPathWithRect:dirtyRect];
[path fill];
// Draw a shadow
CGRect rect = CGRectMake(SHADOW_SIZE, SHADOW_SIZE, dirtyRect.size.width - SHADOW_SIZE, dirtyRect.size.height - SHADOW_SIZE);
path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:32];
[[[UIColor blackColor] colorWithAlphaComponent:0.3f] set];
[path fill];
// Draw shape
rect.origin = CGPointMake(0, 0);
path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:32];
[[UIColor orangeColor] set];
[path fill];
}
#define USE_ALIGNMENT_RECTS 1
#define USE_ALIGNMENT_INSETS 1
#if USE_ALIGNMENT_RECTS
- (CGRect)frameForAlignmentRect:(CGRect)alignmentRect {
CGRect rect = alignmentRect;
rect.size.width += SHADOW_SIZE;
rect.size.height += SHADOW_SIZE;
return rect;
}
- (CGRect)alignmentRectForFrame:(CGRect)frame {
CGRect rect = frame;
rect.size.width = frame.size.width - SHADOW_SIZE;
rect.size.height = frame.size.height - SHADOW_SIZE;
return rect;
}
#endif
#if USE_ALIGNMENT_INSETS
- (UIEdgeInsets)alignmentRectInsets {
return UIEdgeInsetsMake(0, 0, SHADOW_SIZE, SHADOW_SIZE);
}
#endif
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
MyView *view = [[MyView alloc] init];
[self.view addSubview:view];
view.translatesAutoresizingMaskIntoConstraints = NO;
UIView *superview = self.view;
NSDictionary *views = NSDictionaryOfVariableBindings(superview, view);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[superview]-(<=1)-[view(200)]"
options:NSLayoutFormatAlignAllCenterY
metrics:nil
views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[superview]-(<=1)-[view(200)]"
options:NSLayoutFormatAlignAllCenterX
metrics:nil
views:views]];
}
@end
-alignmentRectInsets:
这一功能很好,但是我想找出
-frameForAlignmentRect:
和
-alignmentRectForFrame:
方法有什么问题。
最佳答案
您需要从以下位置更改代码:
view.translatesAutoresizingMaskIntoConstraints = NO;
self.view.translatesAutoresizingMaskIntoConstraints = NO;
- (UIEdgeInsets)alignmentRectInsets
- (CGRect)alignmentRectForFrame:(CGRect)frame
view.frame = CGRectMake(50,50,200,200);
- (void)addConstraints:(NSArray *)constraints
NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[superview]-(<=1)-[view(200)]" options:NSLayoutFormatAlignAllCenterY metrics:nil views:views];
NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[superview]-(<=1)-[view(200)]" options:NSLayoutFormatAlignAllCenterX metrics:nil views:views];
NSArray *constraints = [[NSArray arrayWithArray:horizontalConstraints] arrayByAddingObjectsFromArray:verticalConstraints];
[NSLayoutConstraint activateConstraints:constraints];
关于ios - 为什么-frameForAlignmentRect:和-alignmentRectForFrame:不调用IOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27061654/
我想使用-frameForAlignmentRect:和-alignmentRectForFrame:在我的ios应用程序中指定对齐矩形。但是这个方法没有调用。 我有几乎相同的iOS和Mac代码。它在
我是一名优秀的程序员,十分优秀!