使用UITouches移动UIImageview

时间:2013-01-24 10:19:57

标签: iphone objective-c uiview subclass uitouch

我已经创建了一个uiview,其中我添加了两个uiimageview,其中一个用于背景视图,另一个用于图像的图像视图,我想在触摸时移动。我正在从视图控制器添加此子类uiview 。以下是代码。

@interface CustomSlider : UIView
{
    UIImageView *bgView;
    UIImageView *customSliderImageView;
    float minStartPoint,maxEndPoint;
}
- (id)initWithFrame:(CGRect)frame inView:(UIView*)mainView;
@end

@implementation CustomSlider
- (id)initWithFrame:(CGRect)frame inView:(UIView*)mainView {
if((self = [super init])) {

    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(15, 60, frame.size.width, frame.size.height)];
    contentView.backgroundColor = [UIColor clearColor];
    [mainView addSubview:contentView];

    bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"price_96.png"]];
    bgView.frame = CGRectMake(5, 0, frame.size.width - 10, 8 );
    [contentView addSubview:bgView];

    customSliderImageView= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"price_100.png"]];
    customSliderImageView.frame = CGRectMake(20, 0, 35, frame.size.height);


    [contentView addSubview:customSliderImageView];

    minStartPoint = 0;
    UIPanGestureRecognizer* pgr = [[UIPanGestureRecognizer alloc]
                                   initWithTarget:self
                                   action:@selector(handlePan:)];
    [customSliderImageView addGestureRecognizer:pgr];
    [pgr release];

}
return self;
}
@end

请建议解决方案

1 个答案:

答案 0 :(得分:0)

以下代码用于触摸移动图片,我的代码将对您的案例有所帮助

AppDelegate Classes

**// .h File**

#import <UIKit/UIKit.h>

@class UITouchTutorialViewController;

@interface UITouchTutorialAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    UITouchTutorialViewController *viewController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITouchTutorialViewController *viewController;

@end

//////////////////////////////////////////////////////////////////////////////////

// .m File

#import "UITouchTutorialAppDelegate.h"
#import "UITouchTutorialViewController.h"

@implementation UITouchTutorialAppDelegate

@synthesize window;
@synthesize viewController;


- (void)applicationDidFinishLaunching:(UIApplication *)application {    

    // Override point for customization after app launch    
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];
}


- (void)dealloc {
    [viewController release];
    [window release];
    [super dealloc];
}


@end
//////////////////////////////////////////////////////////////////////////////

UIViewController Classes

// .h file

#import <UIKit/UIKit.h>

@interface UITouchTutorialViewController : UIViewController {
    IBOutlet UIImageView *cloud;
}

@end

// .m File

#import "UITouchTutorialViewController.h"

@implementation UITouchTutorialViewController

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:touch.view];
    cloud.center = location;
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [self touchesBegan:touches withEvent:event];
}

/*
// Override initWithNibName:bundle: to load the view using a nib file then perform additional customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically.
- (void)loadView {
}
*/


/*
// Implement viewDidLoad to do additional setup after loading the view.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}


- (void)dealloc {
    [super dealloc];
}

@end