大佬教程收集整理的这篇文章主要介绍了ios – 静态子视图背景图像效果,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
从本质上讲,如何在子视图后面显示静态背景,作为小窗口而不显示亚麻布.亚麻布只应在细胞周围展示.
您可以下载应用程序,点击飞行模式并亲自尝试.
这是一个截图:
请在sample project查看结果.
为了将来的参考,我将附上以下代码:
ViewController.m
// // OSViewController.m // ScrollMasks // // Created by #%$^Q& on 11/30/12. // Copyright (c) 2012 Demo. All rights reserved. // #import "OSViewController.h" @interface OSViewController () // subviews @property (strong) IBOutlet UIScrollView * scrollView; // all the subviews @property (strong) NSArray * maskedSubviews; // subviews visible at scrollview,we'll update only them @property (strong) NSArray * visibleMaskedSubviews; // updates the views from visibleMaskedSubviews -(void) updateVisibleSubviews; // updates the visibleMaskedSubviews array with the given scrollView offset -(void) updateVisibleSubviewsArrayForOffset:(CGPoint) offset; @end @implementation OSViewController -(void) unused {} #pragma mark - view -(void) viewWillAppear:(BOOL)animated { [self updateVisibleSubviews]; [super viewWillAppear:animated]; } - (void)viewDidLoad { [super viewDidLoad]; /* See -updateVisibleSubviews comment for the class comments */ UIView * newMaskedView = nil; NSMutableArray * newMaskedSubviews = [NSMutableArray array]; const CGSize scrollViewSize = self.scrollView.bounds.size; const int totalSubviews = 10; const float offset = 20.; const float height = 100.; UIImage * maskImage = [UIImage imageNamed:@"PeeringFrog.jpg"]; /* // Uncomment to compare UIImageView * iv = [[UIImageView alloc] initWithFrame:self.scrollView.bounds]; iv.image = maskImage; [self.view insertSubview:iv atIndex:0]; */ // add scrollview subviews for (int i = 0; i < totalSubviews; i++) { CGRect newViewFrame = CGRectMake(offset,offset*(i+1) + height*i,scrollViewSize.width - offset*2,height); newMaskedView = [UIView new]; newMaskedView.frame = newViewFrame; newMaskedView.clipsToBounds = YES; newMaskedView.backgroundColor = [UIColor redColor]; newMaskedView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth; UIImageView * maskImageView = [UIImageView new]; maskImageView.frame = CGRectMake(0,self.scrollView.bounds.size.width,self.scrollView.bounds.size.height); maskImageView.image = maskImage; [newMaskedView addSubview:maskImageView]; [self.scrollView addSubview:newMaskedView]; [newMaskedSubviews addObject:newMaskedView]; } self.scrollView.contentSize = CGSizeMake(scrollViewSize.width,(height+offset)*totalSubviews + offset*2); self.maskedSubviews = [NSArray arrayWithArray:newMaskedSubviews]; [self updateVisibleSubviewsArrayForOffset:self.scrollView.contentOffset]; } -(void) updateVisibleSubviews { [self updateVisibleSubviewsArrayForOffset:self.scrollView.contentOffset]; for (UIView * view in self.visibleMaskedSubviews) { /* TODO: view must be UIView subclass with the imageView initializer and imageView frame update method */ CGPoint viewOffset = [self.view convertPoint:CGPointZero fromView:view]; UIImageView * subview = [[view subviews] objectAtIndex:0]; CGRect subviewFrame = subview.frame; subviewFrame = CGRectMake(-viewOffset.x,-viewOffset.y,subviewFrame.size.width,subviewFrame.size.height); subview.frame = subviewFrame; } } #pragma mark - scrollview delegate & utilities -(void) scrollViewDidScroll:(UIScrollView *)scrollView { [self updateVisibleSubviews]; } -(void) updateVisibleSubviewsArrayForOffset:(CGPoint) offset { NSMutableArray * newVisibleMaskedSubviews = [NSMutableArray array]; for (UIView * view in self.maskedSubviews) { CGRect intersectionRect = CGRectIntersection(view.frame,self.scrollView.bounds); if (NO == CGRectIsNull(intersectionRect)) { [newVisibleMaskedSubviews addObject:view]; } } self.visibleMaskedSubviews = [NSArray arrayWithArray:newVisibleMaskedSubviews]; } #pragma mark - memory - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
ViewController.h
// // OSViewController.h // ScrollMasks // // Created by #%$^Q& on 11/30/12. // Copyright (c) 2012 Demo. All rights reserved. // /* PeeringFrog image is taken (and resized) from Apple sample project "PhotoScroller" */ #import <UIKit/UIKit.h> @interface OSViewController : UIViewController <UIScrollViewDelegate> @end
以上是大佬教程为你收集整理的ios – 静态子视图背景图像效果全部内容,希望文章能够帮你解决ios – 静态子视图背景图像效果所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。