UIScrollView add zoom for the self.view - ios

I need zooming all objects in the view, but lose the position of the objects (UIButton)
- (void)viewDidLoad
{
CGRect scrollViewFrame = CGRectMake(0, 0, 320, 460);
scrollView = [[UIScrollView alloc] initWithFrame:scrollViewFrame];
scrollView.delegate = self;
scrollView.minimumZoomScale = 1;
scrollView.maximumZoomScale = 4.0;
[self.view addSubview:scrollView];
CGSize scrollViewContentSize = CGSizeMake(640, 800);
[scrollView setContentSize:scrollViewContentSize];
NSMutableArray *datos = [[NSMutableArray alloc]initWithObjects:#"ob1",#"ob2",#"ob3",#"ob4", nil];
for (int i = 0; i < [datos count]; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(aMethod)
forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(100, 100*i, 70, 70);
button.tag = i;
[button setTitle: #"Line1" forState: UIControlStateNormal];
button.hidden= NO;
[scrollView addSubview:button];
}
}
-(void)aMethod{
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return scrollView.superview;
}

You should setup a subview container and pass that as the viewForZoomingInScrollView.
The subview should contain your buttons:
self.buttonSubview = [[UIView alloc] initWithFrame:...];
...
[buttonSubview addSubview:button];
...
[scrollView addSubview:buttonSubview];
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return self.buttonSubview;
}

Related

UIView coordinate conversion has an exception. convertPoint: toView: not work well

I try to make a navigation bar indicator view animation. The layout has some problem.
Expects this:
My solution is to convert the position in the navigation bar's bottom position to self.navigationItem.titleView.
The navigation bar indicator view should belong to the view controller. Not the navigation controller. So indicator view is on self.navigationItem.titleView.
While its position is the navigation bar's bottom. Y position is Point (0, 64) exactly.
So a method convertPoint: toView: is what I need .
Here is code:
- (void)setupTitlesView
{
UIView *titlesView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 100, 35)];
self.navigationItem.titleView = titlesView;
CGPoint new_point = [self.view convertPoint: CGPointMake(0, 64) toView: self.navigationItem.titleView];
NSLog(#"\n\n%#\n\n", NSStringFromCGPoint(new_point));
UIView *indicatorView = [[UIView alloc] initWithFrame: CGRectMake(0, new_point.y - 2, 0, 2)];
indicatorView.tag = -1;
self.indicatorView = indicatorView;
NSArray *titles = #[#"商品", #"详情"];
CGFloat width = titlesView.frame.size.width / titles.count;
CGFloat height = titlesView.frame.size.height;
for (NSInteger i = 0; i<titles.count; i++) {
UIButton *button = [[UIButton alloc] initWithFrame: CGRectMake(i*width, 0, width, height)];
button.tag = i;
[button setTitle:titles[i] forState:UIControlStateNormal];
// color , font ...
[titlesView addSubview:button];
// default click
if (i == 0) {
button.enabled = NO;
self.selectedButton = button;
[button.titleLabel sizeToFit];
CGRect tempFrame = self.indicatorView.frame;
tempFrame.size.width = button.titleLabel.frame.size.width;
self.indicatorView.frame = tempFrame;
CGPoint tempCenter = self.indicatorView.center;
tempCenter.x = button.center.x;
self.indicatorView.center = tempCenter;
}
}
[titlesView addSubview: indicatorView];
}
The result prints :
{0, 64}
Apple says:
convertPoint:toView: Converts a point from the receiver’s coordinate
system to that of the specified view.
How to fix it?
If I were you,I would try:
- (void)setupTitlesView
{
UIView *titlesView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 100, 35)];
self.navigationItem.titleView = titlesView;
UIView *indicatorView = [[UIView alloc] initWithFrame: CGRectMake(0, titlesView.frame.size.height - 2, 50, 2)];
indicatorView.tag = -1;
self.indicatorView = indicatorView;
self.indicatorView.backgroundColor = [UIColor blueColor];
NSArray *titles = #[#"商品", #"详情"];
CGFloat width = titlesView.frame.size.width / titles.count;
CGFloat height = titlesView.frame.size.height;
for (NSInteger i = 0; i<titles.count; i++) {
UIButton *button = [[UIButton alloc] initWithFrame: CGRectMake(i*width, 0, width, height)];
button.tag = i+1000;
[button setTitle:titles[i] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
[titlesView addSubview:button];
// default click
if (i == 0) {
button.selected = YES;
}
}
[titlesView addSubview: indicatorView];
}
- (void)buttonClicked:(UIButton *)sender
{
for (int i = 0; i < 2; i++) {
UIButton *button = [self.navigationItem.titleView viewWithTag:(1000+i)];
button.selected = (button.tag == sender.tag);
}
[UIView animateWithDuration:0.3 animations:^{
self.indicatorView.center = CGPointMake(sender.center.x, self.indicatorView.center.y);
}];
}

UIScrollView not scrolling iOS

I am trying to create several UIScrollViews in a single view that scroll horizontally in iOS. This is my code so far:
-(void)updateSection {
[feedLoadingActInd stopAnimating];
feedLoadingActInd.hidden = YES;
builder = [NSMutableArray array];
float xPosition = 0;
float xPosBut = 0;
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 400, self.frame.size.width, self.frame.size.height - 29)];
[scrollView setScrollEnabled:YES];
scrollView.backgroundColor = [UIColor yellowColor];
scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
scrollView.pagingEnabled = YES;
scrollView.delegate = self;
for (int i = 0; i < itemArticleArray.count; i++) {
testButton = [[UIButton alloc] initWithFrame:CGRectMake(xPosBut, 40, 40, 40)];
[testButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[testButton setTitle:#"Test" forState:UIControlStateNormal];
[testButton addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
testButton.backgroundColor = [UIColor blueColor];
xPosBut += testButton.frame.size.width;
NSLog(#"scroll.frame.size.width = %f", scrollView.frame.size.width);
xPosition += 2;
UIView *seperatorView = [[UIView alloc] initWithFrame:CGRectMake(xPosition, 4, 350, scrollView.frame.size.height - 8)];
seperatorView.backgroundColor = [UIColor redColor];
[scrollView addSubview:seperatorView];
xPosition += 350;
[seperatorView addSubview:testButton];
[scrollView addSubview:seperatorView];
[builder addObject:testButton];
}
[self addSubview:scrollView];
[scrollView setUserInteractionEnabled:YES];
[scrollView setContentSize: CGSizeMake(xPosition, scrollView.frame.size.height)];
NSLog(#"scroll.contentsize.width = %f", scrollView.contentSize.width);
}
However, none of the scroll views are actually scrolling, which I am confused about, as there are multiple buttons being added. Also, the buttons that I have added, are not actually doing anything when I press them. They should be running the buttonPressed method, and it doesn't?
Any help would be muchly appreciated!
try this,
float xPosition = 0;
float xPosBut = 0;
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 100,self.view.frame.size.width, self.view.frame.size.height - 29)];
[scrollView setScrollEnabled:YES];
scrollView.backgroundColor = [UIColor yellowColor];
for (int i = 0; i < 10; i++) {
UIButton *testButton = [[UIButton alloc] initWithFrame:CGRectMake(xPosition, 10, scrollView.frame.size.width, 50)];
[testButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[testButton setTitle:#"Test" forState:UIControlStateNormal];
[testButton addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
testButton.backgroundColor = [UIColor blueColor];
xPosition += testButton.frame.size.width;
NSLog(#"xposbut = %f", xPosBut);
NSLog(#"scroll.frame.size.width = %f", scrollView.frame.size.width);
xPosition += scrollView.frame.size.width+2;
UIView *seperatorView = [[UIView alloc] initWithFrame:CGRectMake(xPosition, 4, 2, scrollView.frame.size.height - 8)];
seperatorView.backgroundColor = [UIColor redColor];
[scrollView addSubview:seperatorView];
xPosition +=scrollView.frame.size.width+ 4;
[self.view addSubview:scrollView];
[scrollView addSubview:seperatorView];
[scrollView addSubview:testButton];
[builder addObject:testButton];
}
[scrollView setContentSize: CGSizeMake(xPosition, scrollView.frame.size.height)];
[self.view addSubview:scrollView];
set the for loop condition part as per your requirement.
hope this will help you.

Infinite horizontal UIScrollView with UIButtons

here is what I do at the moment.
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(56, 205, 400, 3000)];
scrollView.delegate = self;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 3.0f,scrollView.frame.size.height * 3.0f);
scrollView.maximumZoomScale = 3.0f;
UIView *zoomView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, scrollView.contentSize.width, scrollView.contentSize.height)];
zoomView.backgroundColor = [UIColor whiteColor];
for (NSInteger index = 0; index < 100; index++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake((scrollView.frame.size.width / 2.0f) - 50.0f, 10.0f + (50.0f * (CGFloat)index), 100.0f, 30.0f);
button.tag = index;
[button setTitle:[NSString stringWithFormat:#"Button %ld", ((long)index + 1)] forState:UIControlStateNormal];
[button addTarget:self action:#selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside];
[zoomView addSubview:button];
}
[scrollView addSubview:zoomView];
[self.view addSubview:scrollView];
The problem is. It's not inifinite. I would like the number 101 to be the number 1, hope you understand. And how could i create them dynamically, with a webservice telling me how much button i will have, what background i should apply to each button, and what text to put under the button. Also the code above is scrolling vertically, but at the end it would be a non zooming horizontal scrolllview. Thanks in advance.
Just set content size of your scrollview
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(56, 205, 400, 320)]; // it is for iphone. set it's resolution if you want to set it for ipad
scrollView.delegate = self;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 3.0f,scrollView.frame.size.height * 3.0f);
scrollView.maximumZoomScale = 3.0f;
UIView *zoomView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, scrollView.contentSize.width, scrollView.contentSize.height)];
zoomView.backgroundColor = [UIColor whiteColor];
for (NSInteger index = 0; index < 100; index++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake((scrollView.frame.size.width / 2.0f) - 50.0f, 10.0f + (50.0f * (CGFloat)index), 100.0f, 30.0f);
button.tag = index;
[button setTitle:[NSString stringWithFormat:#"Button %ld", ((long)index + 1)] forState:UIControlStateNormal];
[button addTarget:self action:#selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside];
[zoomView addSubview:button];
}
[scrollView addSubview:zoomView];
[self.view addSubview:scrollView];
scrollView.contentSize= CGSizeMake((zoomView.frame.size.width) , zoomView.frame.size.height);
set it after your zoom view has been added into scrollView.
I hope it helps you to understand.

uibutton is not getting displayed

I am trying to add uibutton inside the imageview.
But it was not displayed the button.
Even i tried adding it to the uiscrollview and also for self.view.
But nothing were displayed the uibutton
Pls let me know what is the problem
const CGFloat HEIGHT = 1024.0;
const CGFloat WIDTH = 768.0;
#define myViewPortrait CGRectMake(0.0f, 0.0f, 768.0f,1024.0f)
#define myViewLandSacpe CGRectMake(0.0f, 0.0f, 1024.0f,768.0f)
#define kAnimationKey #"animationKey"
-(void)layoutScrollImages
{
UIImageView *view = nil;
NSArray *subviews = [myScrollView subviews];
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (self.view.frame.size.width);
}
}
[myScrollView setContentSize:CGSizeMake((myImagesCount * self.view.frame.size.width), [myScrollView bounds].size.height)];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];
myScrollView = [[UIScrollView alloc] initWithFrame:
CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:myScrollView];
UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(rightSwipeHandle:)];
rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
rightRecognizer.numberOfTouchesRequired = 1;
[rightRecognizer setDelegate:self];
[myScrollView addGestureRecognizer:rightRecognizer];
[rightRecognizer release];
UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(leftSwipeHandle:)];
leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
leftRecognizer.numberOfTouchesRequired = 1;
[leftRecognizer setDelegate:self];
[myScrollView addGestureRecognizer:leftRecognizer];
[leftRecognizer release];
[myScrollView setBackgroundColor:[UIColor blackColor]];
[myScrollView setCanCancelContentTouches:NO];
myScrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
myScrollView.clipsToBounds = YES;
myScrollView.scrollEnabled = YES;
myScrollView.pagingEnabled = YES;
myScrollView.delegate = self;
myImagesCount = 5;
myScrollView.showsHorizontalScrollIndicator=NO;
myScrollView.showsVerticalScrollIndicator=NO;
for (int i = 1; i <= myImagesCount; i++)
{
NSString *imageName = [NSString stringWithFormat:#"screen-%d.jpg", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
CGRect rect = imageView.frame;
rect.size.height = myScrollView.frame.size.height;
NSLog(#"%d -----",self.view.frame.size.width);
rect.size.width = myScrollView.frame.size.width;
imageView.frame = rect;
imageView.tag = i;
[myScrollView addSubview:imageView];
[imageView release];
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(buttonHandler) forControlEvents:UIControlEventAllEvents];
[button setTitle:#"point" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, 100.0, 40.0);
button.backgroundColor = [UIColor blackColor];
[self.view addSubview:button];
[self layoutScrollImages];
[super viewDidLoad];
}
Based off your question, you'd like to add the UIButton as a subview to the UIImageView - I'm guessing in the for loop? Immediate problem I see is that the actual button is being generated outside of the for loop. I think your for loop is intended to look like this:
for (int i = 1; i <= myImagesCount; i++)
{
NSString *imageName = [NSString stringWithFormat:#"screen-%d.jpg", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(buttonHandler) forControlEvents:UIControlEventAllEvents];
[button setTitle:#"point" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, 100.0, 40.0);
button.backgroundColor = [UIColor blackColor];
CGRect rect = imageView.frame;
rect.size.height = myScrollView.frame.size.height;
NSLog(#"%d -----",self.view.frame.size.width);
rect.size.width = myScrollView.frame.size.width;
imageView.frame = rect;
imageView.tag = i;
[imageView addSubview:button];
[myScrollView addSubview:imageView];
[imageView release];
}
Try this
Declare button globally and set
[self layoutScrollImages];
[self.view bringSubViewToFront:button];

UIScrollView with a large number of UIButtons

What I want is a UIView with lots of UIButtons in it. They get placed and arranged according to data stored in an NSArray. Because there are quite a lot of buttons they don't fit on the screen all at once. The user should be able to zoom out to see all the buttons or to zoom in to see details (the label on them) and to easily select them.
I tried two different approaches:
1) I constructed a UIView subclass, put the buttons in it and an instance of this View inside a UIScrollview.
Effect: I can access all Buttons via their tag and scrolling and zooming works fine. BUT I can't get the buttons to handle any events (press on them)...
2) I wrote a UIViewController with exactly the same functionality and added an instance of it to the UIScrollView.
Effect: I can press the buttons now, but scrolling and zooming have stopped to work.
Here the relevant Code of the View:
- (UIView *)initWithArray:(NSArray *)nArray{
self = [super init];
if (self) {
int count = [nArray count];
for (int i=0; i<count; i++) {
UIButton *button = [[UIButton alloc]
initWithFrame:(__some Code to place the Button__);
button.tag = i+1;
NSString *title = [[NSString alloc] initWithFormat:__code for generating Title__];
[button setTitle:title forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:14];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
[self addSubview:button];
}
}
return self;
}
And the Code for the matrixController:
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *nArray = [[NSMutableArray alloc] __some code___];
int count = [nArray count];
for (int i=0; i<count; i++) {
UIButton *button = [[UIButton alloc]
initWithFrame:CGRectMake(__some Code to place the Button__];
button.tag = i+1;
NSString *title = [[NSString alloc] initWithFormat:__code for generating Title__];
[button setTitle:title forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:14];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:button];
}
}
And the code for the ScrollViewController:
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 768, 970)];
[self.view addSubview:scrollView];
[scrollView setBackgroundColor:[UIColor blackColor]];
//Zooming
[scrollView setMinimumZoomScale:0.25];
[scrollView setMaximumZoomScale:4.0];
[scrollView setDelegate:self];
// constructing the view
[scrollView addSubview:chartView];
[scrollView bringSubviewToFront:chartView];
OR
[scrollView addSubview:[matrixController view]];
How can I get this to work??
I'm able to get a scroll view containing multiple buttons to pan and zoom just fine, with the buttons still handling touch events:
- (void)didTapButton:(UIButton *)button
{
NSLog(#"Button %ld", (long)button.tag);
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
scrollView.delegate = self;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 3.0f, scrollView.frame.size.height * 3.0f);
scrollView.maximumZoomScale = 3.0f;
UIView *zoomView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, scrollView.contentSize.width, scrollView.contentSize.height)];
zoomView.backgroundColor = [UIColor whiteColor];
for (NSInteger index = 0; index < 100; index++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake((scrollView.frame.size.width / 2.0f) - 50.0f, 10.0f + (50.0f * (CGFloat)index), 100.0f, 30.0f);
button.tag = index;
[button setTitle:[NSString stringWithFormat:#"Button %ld", ((long)index + 1)] forState:UIControlStateNormal];
[button addTarget:self action:#selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside];
[zoomView addSubview:button];
}
[scrollView addSubview:zoomView];
[self.view addSubview:scrollView];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return [scrollView.subviews objectAtIndex:0];
}
EDIT: I said not to rely on tag in my comment below, yet I'm doing in here. :) It's merely so I could log the button number, so that part should be ignored.
for (int i=0; i<10; i++)
{
UIButton *scrollingbutton_outlet = [[UIButton alloc] init];
scrollingbutton_outlet = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *img = [UIImage imageNamed:#"box_normal.png"];
scrollingbutton_outlet.frame = CGRectMake(0, 100, img.size.width, img.size.height);
[scrollingbutton_outlet setTitle:[NSString stringWithFormat:#"%d",i+1] forState: UIControlStateNormal];
scrollingbutton_outlet.tag=i+1;
[buttonArray addObject:[NSString stringWithFormat:#"%d",i+1]];
buttonArray = [[NSMutableArray alloc] init];
[scrollingbutton_outlet setBackgroundImage:img forState:UIControlStateNormal];
[scrollingbutton_outlet addTarget:self
action:#selector(scrollbuttonpress:)
forControlEvents:UIControlEventTouchUpInside];
scrollingbutton_outlet.frame = CGRectMake(img.size.width*i,0, img.size.width, scrollviewoutlet.frame.size.height);
[scrollviewoutlet addSubview:scrollingbutton_outlet];
width = scrollingbutton_outlet.frame.size.width;
height = scrollingbutton_outlet.frame.size.height;
scrollviewoutlet.contentSize = CGSizeMake(width*i+scrollingbutton_outlet.bounds.size.width+5, height);
}

Resources