Ios obj-c中的静态背景图像位置(类似于背景附件:固定)

Ios obj-c中的静态背景图像位置(类似于背景附件:固定),ios,objective-c,css,Ios,Objective C,Css,我正在尝试创建一个视图,无论它如何移动,都会将其背景图像保持在同一位置。因此,如果拖动视图,背景将保持不变,类似于css3中的background attachment:fixed 我的问题是clipstobunds=YES似乎不起作用。图像仍然占据整个屏幕,不会被剪裁到父帧。有什么想法吗 @implementation StaticBackgroundView { UIImageView *imageView; } // Must be init'd from code or won

我正在尝试创建一个视图,无论它如何移动,都会将其背景图像保持在同一位置。因此,如果拖动视图,背景将保持不变,类似于css3中的
background attachment:fixed

我的问题是
clipstobunds=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移动时,也必须调整背景图像中心

在下面给出的示例中,每当StaticBackgroundView在PanGesture更新期间移动时,都会调用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并在下面显示静态图像:

静态背景视图

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

如果我关心的视图是平移的目标,这没关系,但是如果另一个视图完全是位置变化的原因呢?或者如果视图正在设置动画,你在吗?有什么办法可以解决这个问题吗?如果视图是动画,那么你就不走运了。您只能获取开始和结束动画转换数据,而不能在动画制作时获取。如果另一个视图导致更改,则相应地调整handlePan。其他视图的平移值将调整静态背景视图。
#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