【发布时间】:2015-03-22 09:41:03
【问题描述】:
我知道有很多关于这个主题的文档,并且从我所阅读的内容中我学到了很多东西。我的问题是,我开始在视图控制器中创建自定义 UIView,现在当我尝试将其作为 UIView 自己的类,以便在工作应用程序中调用和使用时,我很困惑什么我需要做才能实现它。
该类的总体目标是能够选择一个按钮一个视图控制器,它将实例化自定义视图,然后显示。用户的视觉效果是他们点击按钮,然后一个窗口会从按钮的边界动画出现(虽然下面的代码目前使用左上角的定位),然后显示开关和标签的组合,沿着带有后退和保存按钮。
您可以从我的代码中看到我需要实现这个过程,我已经设法将其实现为一个函数(同样,这是通过在视图控制器中创建它)。我现在所追求的,不能完全理解的是要导入自定义视图,然后在主视图控制器按钮的操作上,将视图添加到 self.view。
- (void)makeBox{
//view bounds
CGFloat viewWidth = CGRectGetWidth(self.view.frame);
CGFloat viewHeight = CGRectGetHeight(self.view.frame);
//red box x, y & size
CGFloat startx = (viewWidth / 100) * 10;
CGFloat starty = (viewHeight /100) * 20;
CGFloat width = (viewWidth / 100) * 80;
CGFloat height = (viewHeight /100) * 70;
CGRect view1Frame = CGRectMake(startx, starty, width, height);
//label & switch frame
CGRect labelMR = CGRectMake(10, ((height / 100) * 12), ((width / 100) * 80) - 7.5, 25);
CGRect switchMR = CGRectMake(((width / 100) * 80) + 7.5, ((height / 100) * 11), ((width / 100) * 10), 15);
//this is repeated for 6 other labels & switches
CGRect backButtonR = CGRectMake(5, 5, 50, 35);
CGRect saveButtonR = CGRectMake((width - 50), 5, 50, 35);
if (!self.view1) {
self.switchM = [[UISwitch alloc] initWithFrame:switchMR];
self.switchM.tag = 10;
if(self.monday){ //self.monday refers to a BOOL property of the viewcontroller that this was originally made in
self.switchM.on = true;
} else{
self.switchM.on = false;
}
[self.switchM addTarget:self action:@selector(switched:) forControlEvents:UIControlEventTouchUpInside];
// this switch instantiation process is repeated 6 other times
self.backButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.backButton addTarget:self
action:@selector(hideBox)
forControlEvents:UIControlEventTouchUpInside];
[self.backButton setTitle:@"Back" forState:UIControlStateNormal];
self.backButton.frame = backButtonR;
self.saveButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.saveButton addTarget:self
action:@selector(daysChanged)
forControlEvents:UIControlEventTouchUpInside];
[self.saveButton setTitle:@"Save" forState:UIControlStateNormal];
self.saveButton.frame = saveButtonR;
self.labelM = [[UILabel alloc] initWithFrame:labelMR];
self.labelM.layer.masksToBounds = YES;
self.labelM.layer.cornerRadius = 5;
self.labelM.layer.borderColor = [UIColor blackColor].CGColor;
self.labelM.layer.borderWidth = .5;
self.labelM.text = @" Monday";
self.labelM.backgroundColor = [UIColor whiteColor];
//again - repeated 6 times
//use this starting frame to make the 'popover' appear small at first
CGRect startFrame = CGRectMake(10, 10, 10, 10);
self.view1 = [[UIView alloc] initWithFrame:startFrame];
[self.view addSubview:self.view1];
[UIView animateWithDuration:0.5 animations:^(void){
self.view1.layer.cornerRadius = 10;
self.view1.layer.borderColor = [UIColor blackColor].CGColor;
self.view1.layer.borderWidth = .5;
[self.view1 setBackgroundColor:[UIColor lightGrayColor]];
self.view1.frame = view1Frame;
} completion:^(BOOL finished){
[self.view1 addSubview:self.labelM];
[self.view1 addSubview:self.switchM];
//repeat 6 times for other labels & switches
}];
}
else {
[UIView animateWithDuration:0.3 animations:^() {
self.view1.frame = view1Frame;
} completion:^(BOOL finished) {
self.labelM.frame = labelMR;
self.switchM.frame = switchMR;
//repeated for the other 6
self.backButton.frame = backButtonR;
self.saveButton.frame = saveButtonR;
}];
}
}
-(void) hideBox{
//back button was selected
[UIView animateWithDuration:0.5 animations:^(void){
[self.view1 removeFromSuperview];
self.view1 = nil;
}];
}
我了解,当我通过代码执行/调用此操作时,我需要覆盖 init 或 initWithFrame:。我是否通过 initWithFrame: 包含视图控制器边界,可以计算自定义视图,以及如何处理动画?
我已经设法设置了委托协议,当按下保存按钮时会发出通知。这部分我理解,只是我不清楚的其余部分。
【问题讨论】:
标签: ios objective-c uiview