0

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

1 回答 1

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
于 2013-01-24T10:23:30.363 回答