Whenever the StaticBackgroundView gets moved - the background image center must also be adjusted.
In the example given below, updateBackgroundImage is called every time the StaticBackgroundView gets moved during a PanGesture update.
-(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));
}
If you move the StaticBackgroundView towards to right, the image gets moved towards the left and appears to remain static beneath the StaticBackgroundView.
Example code below which allows you to use your finger to move the StaticBackgroundView about and show a static image beneath:
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