简体   繁体   English

使用UITouches移动UIImageview

[英]Move UIImageview using the UITouches

I have subclassed a uiview ,in which i am adding two uiimageview,with one view for the background and another image view for an image which one i would like to move on touch.I am adding this subclass uiview from a view controller.The following is the code. 我将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

Please suggest a solution 请提出解决方案

Following code is use for move image on your touch, My code will helpful for your case 以下代码用于触摸时移动图像,“我的代码”将对您的情况有所帮助

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM