【问题标题】:Move UIImageview using the UITouches使用 UITouches 移动 UIImageview
【发布时间】:2013-01-08 01:32:47
【问题描述】:

我对 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

【问题讨论】:

    标签: iphone objective-c uiview subclass uitouch


    【解决方案1】:
    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
    

    【讨论】:

    • 关键是touchesBegan:withEvent:实现。
    • -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; CGPoint location = [touch locationInView:touch.view]; cloud.center = 位置; }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 2012-02-25
    • 2012-04-21
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多