0

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

我的问题是这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);
}
4

1 回答 1

0

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
于 2013-09-29T03:01:10.700 回答