obj-c中的静态背景图像位置(类似于background-attachment:fixed)

我想创造一个观点,即不pipe它如何移动,都将保持其在相同位置的背景图像。 所以,如果视图被拖动,背景保持放置,类似于css3中的background-attachment:fixed

我的问题是, clipsToBounds = YES似乎不工作。 图像仍然占据了整个屏幕,并没有被剪切到父母的框架。 有任何想法吗?

 @implementation StaticBackgroundView { UIImageView *imageView; } // Must be init'd from code or won't work - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myImage.png"]]; imageView.clipsToBounds = YES; self.clipsToBounds = YES; [self addSubview:imageView]; [self sendSubviewToBack:imageView]; } return self; } - (void) setFrame:(CGRect)frame { // Call the parent class to move the view [super setFrame:frame]; // Do your custom code here. NSLog(@"Setframe"); imageView.frame = CGRectMake(-1*frame.origin.x, frame.origin.y, imageView.frame.size.width, imageView.frame.size.height); } 

每当StaticBackgroundView被移动时 – 背景图像中心也必须被调整。

在下面给出的示例中, 每当在PanGesture更新期间移动StaticBackgroundView时, 都会调用updateBackgroundImage。

 -(void)updateBackgroundImage { imageView.center = CGPointMake(( (imageView.image.size.width*0.5) - self.center.x) + (self.bounds.size.width*0.5) , ((imageView.image.size.height*0.5) - self.center.y) + (self.bounds.size.height*0.5)); } 

如果将StaticBackgroundView向右移动,则图像将向左移动,并在StaticBackgroundView下方保持静态。

下面的示例代码允许您使用手指移动StaticBackgroundView并在下面显示静态图像:

StaticBackgroundView.h

 #import <UIKit/UIKit.h> @interface StaticBackgroundView : UIView -(void)updateBackgroundImage; @end 

StaticBackgroundView.m

 #import "StaticBackgroundView.h" @implementation StaticBackgroundView { UIImageView *imageView; } // Must be init'd from code or won't work - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { NSLog(@"StaticBackgroundView:initWithFrame"); if([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPhone) { if ([[UIScreen mainScreen] bounds].size.height == 568) { NSLog(@"iPhone5 image"); imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myImage-568h"]]; } else { NSLog(@"iPhone4 image"); imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myImage"]]; } } self.clipsToBounds = YES; [self updateBackgroundImage]; [self addSubview:imageView]; [self sendSubviewToBack:imageView]; } return self; } -(void)updateBackgroundImage { imageView.center = CGPointMake(( (imageView.image.size.width*0.5) - self.center.x) + (self.bounds.size.width*0.5) , ((imageView.image.size.height*0.5) - self.center.y) + (self.bounds.size.height*0.5)); } 

ViewController.m

 #import "ViewController.h" #import "StaticBackgroundView.h" @interface ViewController () @property (strong, nonatomic) StaticBackgroundView *outletStaticView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.outletStaticView = [[StaticBackgroundView alloc] initWithFrame:CGRectMake(100, 100, 120, 150)]; [self.view addSubview:self.outletStaticView]; UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)]; [self.outletStaticView addGestureRecognizer:panGestureRecognizer]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)handlePan:(UIPanGestureRecognizer *)gestureRecognizer { if([gestureRecognizer state] == UIGestureRecognizerStateChanged) { CGPoint translation = [gestureRecognizer translationInView:self.view]; gestureRecognizer.view.center = CGPointMake(gestureRecognizer.view.center.x + translation.x, gestureRecognizer.view.center.y + translation.y); [gestureRecognizer setTranslation:CGPointMake(0, 0) inView:self.view]; StaticBackgroundView * theView = (StaticBackgroundView*)gestureRecognizer.view; [theView updateBackgroundImage]; } } @end