工作中会遇到各种各样的问题,同时会有各种各样的好代码值得记录~~~现在就开始吧~~~
1,当b视图弹出后到a视图页面时会执行到的方法:
- (void)viewWillAppear:(BOOL)animated
2,设置头部导航的显示还是隐藏
[self.navigationControllersetNavigationBarHidden:NOanimated:YES];
3,设置uiwebview的放大缩小
self.uiWebView.scalesPageToFit =YES;
4,跳转到下一个视图,以及跳出当前视图
[self.navigationControllerpushViewController:showWebanimated:YES];[self.navigationControllerpopViewControllerAnimated:YES];
5,添加子视图和去除当前子视图
[slef addSubview:c1];
[self removeFromSuperview];
6,按钮被按下时发光效果以及执行对应的方法
mishBtn.showsTouchWhenHighlighted =YES; //指定按钮被按下时发光
[mishBtnsetTitleColor:[UIColorwhiteColor]
forState:UIControlStateNormal];//此时选中
[mishBtnaddTarget:selfaction:@selector(mishAction)forControlEvents:UIControlEventTouchUpInside];
[mishBtnsetTitleColor:[UIColorwhiteColor]forState:UIControlStateNormal];//此时选中
[mishBtnsetBackgroundImage:[UIImageimageNamed:@"btn_sh.png"]forState:UIControlStateNormal];
[passshBtnsetTitleColor:[UIColorcolorWithRed:(220/255.0)green:(220/255.0)blue:(220/255.0)alpha:1]forState:UIControlStateNormal];//此时未被选中
[passshBtnsetBackgroundImage:[UIImageimageNamed:@"btn_w.png"]forState:UIControlStateNormal];
7.uiview添加阴影以及解决滑动时卡顿问题
//消除滑动时卡顿现象
[[imgView layer] setShadowPath:[UIBezierPath bezierPathWithRect:imgView.bounds].CGPath];
//设置uiview阴影
[[imgView layer] setShadowOffset:CGSizeMake(1, 1)];
[[imgView layer] setShadowRadius:5];
[[imgView layer] setShadowOpacity:1];
[[imgView layer] setShadowColor:[UIColor blackColor].CGColor];
8.uiview添加背景图的几种方法
方法一:
这种方法内存占的相对较少。
UIImage *image = [UIImage imageNamed:imageName];
imgView.layer.contents = (id) image.CGImage;
方法二:
把图案以颜色的形式,付给背景色,达到实现背景色的效果
UIColor *bgColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"bgImg.png"];
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,480)];
[myView setBackGroundColor:bgColor];
9、 若将一个view作为子视图添加到window中,则当设备的方向变换时,该视图不会随之变化,也就是所不会响应设备的方向变化事件。如:“图片放大视图”,“报纸选择视图”...要使其响应方向变化事件,最好是将该视图添加到一个视图中而不是window中。
[avatarImageView convertRect:avatarImageView.bounds toView:window];
10.UIView中的坐标转换
// 将像素point由point所在视图转换到目标视图view中,返回在目标视图view中的像素值
- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view;
// 将像素point从view中转换到当前视图中,返回在当前视图中的像素值
- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view;
// 将rect由rect所在视图转换到目标视图view中,返回在目标视图view中的rect
- (CGRect)convertRect:(CGRect)rect toView:(UIView *)view;
// 将rect从view中转换到当前视图中,返回在当前视图中的rect
- (CGRect)convertRect:(CGRect)rect fromView:(UIView *)view;
例把UITableViewCell中的subview(btn)的frame转换到 controllerA中
// controllerA 中有一个UITableView, UITableView里有多行UITableVieCell,cell上放有一个button
// 在controllerA中实现:
CGRect rc = [cell convertRect:cell.btn.frame toView:self.view];
// 或
CGRect rc = [self.view convertRect:cell.btn.frame fromView:cell];
// 此rc为btn在controllerA中的rect
// 或当已知btn时:
CGRect rc = [btn.superview convertRect:btn.frame toView:self.view];
// 或
CGRect rc = [self.view convertRect:btn.frame fromView:btn.superview];
11.视图中手势单击双击共存
- (void)viewDidLoad{
[super viewDidLoad];
//点击事件
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fun1)];
//单点触摸
tap.numberOfTouchesRequired = 1;
//点击几次,如果是1就是单击
tap.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:tap];
UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fun2)];
tap2.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:tap2];
//如果满足第二次 第一次的就取消
[tap requireGestureRecognizerToFail:tap2];
}
12,禁止view controller的旋转以及旋转的控制,手动添加
//禁止旋转
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(BOOL)shouldAutorotate
{
return NO;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
//旋转方向控制
|
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
-(BOOL)shouldAutorotate
{
return YES;
}
|
---------------------------------继续添加中~~~~-------------------------