ScrollViewとScrollViewでスクロールする画像があるサンプルを作成しました。画像の1つをクリックすると、UIScrollViewの下で押されたボタン画像がUILabelViewの形式で表示されます。私が理解したいのは、ラベルをUIImageViewに変更する方法です。したがって、押されたときにボタン%dと言う代わりに。自分が決めた画像を見せたいです。
Kitchens.h
#import <UIKit/UIKit.h>
#import "SlideMenuView.h"
@interface Kitchens : UIViewController {
IBOutlet UIScrollView *windows;
SlideMenuView *slideMenuView;
UILabel *screenLabel;
}
@property (nonatomic, retain) UIScrollView *windows;
@property (nonatomic, retain) SlideMenuView *slideMenuView;
@property (nonatomic, retain) UILabel *screenLabel;
@end
Kitchens.m
#import "Kitchens.h"
#import "SlideMenuView.h"
@interface Kitchens ()
@end
@implementation Kitchens
@synthesize windows, slideMenuView, screenLabel;
#pragma mark -
#pragma mark <Touches Began/Moved/Ended/Cancelled Methods>
- (void)viewDidLoad {
// Create and position a label
screenLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 200.0f, [windows bounds].size.width-20.0f, 20.0f)];
[windows addSubview:screenLabel];
// Create buttons for the sliding menu. For simplicity I create 5 standard buttons.
NSMutableArray* buttonArray = [[NSMutableArray alloc] init];
for(int i = 0; i < 5; i++)
{
// Rounded rect is nice
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// Give the buttons a width of 100 and a height of 30. The slide menu will take care of positioning the buttons.
// If you don't know that 100 will be enough, use my function to calculate the length of a string. You find it on my blog.
[btn setFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];
[btn setTitle:[NSString stringWithFormat:@"Button %d", i+1] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[btn setImage:[UIImage imageNamed:@"test.jpg"] forState:UIControlStateNormal];
[buttonArray addObject:btn];
// Rounded rect is nice
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// Give the buttons a width of 100 and a height of 30. The slide menu will take care of positioning the buttons.
// If you don't know that 100 will be enough, use my function to calculate the length of a string. You find it on my blog.
[btn1 setFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];
[btn1 setTitle:[NSString stringWithFormat:@"Button2"] forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[btn1 setImage:[UIImage imageNamed:@"pia05733-640-480.jpg"] forState:UIControlStateNormal];
[buttonArray addObject:btn1];
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// Give the buttons a width of 100 and a height of 30. The slide menu will take care of positioning the buttons.
// If you don't know that 100 will be enough, use my function to calculate the length of a string. You find it on my blog.
[btn2 setFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];
[btn2 setTitle:[NSString stringWithFormat:@"Button3"] forState:UIControlStateNormal];
[btn2 addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[btn2 setImage:[UIImage imageNamed:@"pia05733-640-480.jpg"] forState:UIControlStateNormal];
[buttonArray addObject:btn2];
}
// initialize the slide menu by passing a suitable frame, background color and an array of buttons.
slideMenuView = [[SlideMenuView alloc] initWithFrameColorAndButtons:CGRectMake(10.0f, 30.0f, [windows bounds].size.width-20.0f, 100.0f) backgroundColor:[UIColor blackColor] buttons:buttonArray];
// Add the slide menu to the window.
[windows addSubview:slideMenuView];
[super viewDidLoad];
}
- (IBAction)buttonPressed:(id)sender {
screenLabel.text = ((UIButton*)sender).currentTitle;
UIButton *btn2 = (UIButton*)sender;
UIImage *image = btn2.currentImage;
// Set the image in the image view
self.imageView.image = image;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
@end
SlideMenuView、h
#import <UIKit/UIKit.h>
@interface SlideMenuView : UIView <UIScrollViewDelegate> {
UIScrollView *menuScrollView;
UIImageView *rightMenuImage;
UIImageView *leftMenuImage;
NSMutableArray *menuButtons;
}
-(id) initWithFrameColorAndButtons:(CGRect)frame backgroundColor:(UIColor*)bgColor buttons:(NSMutableArray*)buttonArray;
@property (nonatomic, retain) UIScrollView* menuScrollView;
@property (nonatomic, retain) UIImageView* rightMenuImage;
@property (nonatomic, retain) UIImageView* leftMenuImage;
@property (nonatomic, retain) NSMutableArray* menuButtons;
@end
SlideMenuView.m
#import "SlideMenuView.h"
@implementation SlideMenuView
@synthesize menuScrollView, rightMenuImage, leftMenuImage;
@synthesize menuButtons;
-(id) initWithFrameColorAndButtons:(CGRect)frame backgroundColor:(UIColor*)bgColor buttons:(NSMutableArray*)buttonArray {
if (self = [super initWithFrame:frame]) {
// Initialize the scroll view with the same size as this view.
menuScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, frame.size.width, frame.size.height)];
// Set behaviour for the scrollview
menuScrollView.backgroundColor = bgColor;
menuScrollView.showsHorizontalScrollIndicator = FALSE;
menuScrollView.showsVerticalScrollIndicator = FALSE;
menuScrollView.scrollEnabled = YES;
menuScrollView.bounces = FALSE;
// Add ourselves as delegate receiver so we can detect when the user is scrolling.
menuScrollView.delegate = self;
// Add the buttons to the scrollview
menuButtons = buttonArray;
float totalButtonWidth = 0.0f;
for(int i = 0; i < [menuButtons count]; i++)
{
UIButton *btn = [menuButtons objectAtIndex:i];
// Move the buttons position in the x-demension (horizontal).
CGRect btnRect = btn.frame;
btnRect.origin.x = totalButtonWidth;
[btn setFrame:btnRect];
// Add the button to the scrollview
[menuScrollView addSubview:btn];
// Add the width of the button to the total width.
totalButtonWidth += btn.frame.size.width;
}
// Update the scrollview content rect, which is the combined width of the buttons
[menuScrollView setContentSize:CGSizeMake(totalButtonWidth, self.frame.size.height)];
[self addSubview:menuScrollView];
}
return self;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// if the offset is less than 3, the content is scrolled to the far left. This would be the time to show/hide
// an arrow that indicates that you can scroll to the right. The 3 is to give it some "padding".
if(scrollView.contentOffset.x <= 3)
{
NSLog(@"Scroll is as far left as possible");
}
// The offset is always calculated from the bottom left corner, which means when the scroll is as far
// right as possible it will not give an offset that is equal to the entire width of the content. Example:
// The content has a width of 500, the scroll view has the width of 200. Then the content offset for far right
// would be 300 (500-200). Then I remove 3 to give it some "padding"
else if(scrollView.contentOffset.x >= (scrollView.contentSize.width - scrollView.frame.size.width)-3)
{
NSLog(@"Scroll is as far right as possible");
}
else
{
// The scoll is somewhere in between left and right. This is the place to indicate that the
// use can scroll both left and right
}
}
- (void)drawRect:(CGRect)rect {
// Drawing code
}
- (void)dealloc {
//[menuButtons release];
//[rightMenuImage release];
//[leftMenuImage release];
//[menuScrollView release];
// [super dealloc];
}
@end