Resizing a View. Not working the way I planned - iphone

I'm resizing a UIView and a UIScrollView based on the number of pixels that UITextViews are being moved. I have all of the pixels being stored in int variables. At the end of my method to populate all of these text views I'm using this:
viewHeight += viewPixelsToBeAdded;
viewHeight -= viewPixelsToBeRemoved;
detailsView.frame = CGRectMake(0, 20, 320, viewHeight);
realDetailsView.frame = CGRectMake(0, 20, 320, viewHeight);
viewHeight is the constant view height which is 1475. I'm using viewPixelsToBeAdded and viewPixelsToBeRemoved to hold the total number of pixels that the view needs to be resized. When I use the above code... my scrollView (detailsView) just stops scrolling. Altogether. I have my objects inside of a view. That view is inside of a scrollView. Is there an easier way to go about resizing this stuff? Why does my scrollView stop scrolling when I increase or decrease its height? Any help would be greatly appreciated. I can post the entire populateLabels method if need be. I kinda feel like I'm taking the long route with it anyway.
-(void) populateLabels {
NSString *noInfo = (#"No Information Available");
lblCgName.text = _Campground.campground;
NSString *cgLoc = _Campground.street1;
NSString *cgCity = _Campground.city;
NSString *cgState = _Campground.state1;
NSString *cgCountry = _Campground.country;
NSString *cgZipPostal = _Campground.zipPostal;
cgLoc = [[cgLoc stringByAppendingString:#", "] stringByAppendingString:cgCity];
cgLoc = [[cgLoc stringByAppendingString:#", "] stringByAppendingString:cgState];
cgLoc = [[cgLoc stringByAppendingString:#", "] stringByAppendingString:cgCountry];
cgLoc = [[cgLoc stringByAppendingString:#" "] stringByAppendingString:cgZipPostal];
lblCgLoc.text = cgLoc;
double dRate = [_Campground.regRate1 doubleValue];
double dRate2 = [_Campground.regRate2 doubleValue];
NSString *rate = [[NSString alloc] initWithFormat:#"$%0.2f",dRate];
NSString *rate2 = [[NSString alloc] initWithFormat:#"$%0.2f",dRate2];
if ([rate2 isEqualToString:#"$0.00"]) {
lblRate.text = rate;
} else {
rate = [[rate stringByAppendingString:#" - "] stringByAppendingString:rate2];
lblRate.text = rate;
}
double dPaRate = [_Campground.paRate1 doubleValue];
double dPaRate2 = [_Campground.paRate2 doubleValue];
NSString *paRate = [[NSString alloc] initWithFormat:#"$%0.2f",dPaRate];
NSString *paRate2 = [[NSString alloc] initWithFormat:#"$%0.2f",dPaRate2];
if ([paRate2 isEqualToString:#"$0.00"]) {
lblPaRate.text = paRate;
} else {
paRate = [[paRate stringByAppendingString:#" - "] stringByAppendingString:paRate2];
lblPaRate.text = paRate;
}
lblLocal1.text = _Campground.localPhone1;
lblLocal2.text = _Campground.localPhone2;
lblTollFree.text = _Campground.tollFree;
lblFax.text = _Campground.fax;
lblEmail.text = _Campground.email;
lblWebsite.text = _Campground.website;
NSString *gps = _Campground.latitude;
NSString *longitude = _Campground.longitude;
gps = [[gps stringByAppendingString:#", "] stringByAppendingString:longitude];
lblGps.text = gps;
int viewHeight = 1475;
int textViewDefaultHeight = 128;
int newTextViewHeight = 0;
int highlightsPixelsRemoved = 0;
int highlightsPixelsAdded = 0;
int notesPixelsRemoved = 0;
int notesPixelsAdded = 0;
int directionsPixelsRemoved = 0;
int directionsPixelsAdded = 0;
int rentalsPixelsRemoved = 0;
int rentalsPixelsAdded = 0;
int viewPixelsToBeRemoved = 0;
int viewPixelsToBeAdded = 0;
//----------------------------------------------------------------------------
txtHighlights.text = _Campground.highlights;
[self fitFrameToContent:txtHighlights];
newTextViewHeight = txtHighlights.contentSize.height;
if (newTextViewHeight > textViewDefaultHeight) {
highlightsPixelsAdded = newTextViewHeight - textViewDefaultHeight;
viewPixelsToBeAdded += highlightsPixelsAdded;
txtTentsHead.frame = CGRectOffset(txtTentsHead.frame, 0, highlightsPixelsAdded);
txtTents.frame = CGRectOffset(txtTents.frame, 0, highlightsPixelsAdded);
txtNotesHead.frame = CGRectOffset(txtNotesHead.frame, 0, highlightsPixelsAdded);
txtNotes.frame = CGRectOffset(txtNotes.frame, 0, highlightsPixelsAdded);
txtDirectionsHead.frame = CGRectOffset(txtDirectionsHead.frame, 0, highlightsPixelsAdded);
txtDirections.frame = CGRectOffset(txtDirections.frame, 0, highlightsPixelsAdded);
txtRentalsHead.frame = CGRectOffset(txtRentalsHead.frame, 0, highlightsPixelsAdded);
txtRentals.frame = CGRectOffset(txtRentals.frame, 0, highlightsPixelsAdded);
} else if (newTextViewHeight < textViewDefaultHeight) {
highlightsPixelsRemoved = textViewDefaultHeight - newTextViewHeight;
viewPixelsToBeRemoved += highlightsPixelsRemoved;
txtTentsHead.frame = CGRectOffset(txtTentsHead.frame, 0, -highlightsPixelsRemoved);
txtTents.frame = CGRectOffset(txtTents.frame, 0, -highlightsPixelsRemoved);
txtNotesHead.frame = CGRectOffset(txtNotesHead.frame, 0, -highlightsPixelsRemoved);
txtNotes.frame = CGRectOffset(txtNotes.frame, 0, -highlightsPixelsRemoved);
txtDirectionsHead.frame = CGRectOffset(txtDirectionsHead.frame, 0, -highlightsPixelsRemoved);
txtDirections.frame = CGRectOffset(txtDirections.frame, 0, -highlightsPixelsRemoved);
txtRentalsHead.frame = CGRectOffset(txtRentalsHead.frame, 0, -highlightsPixelsRemoved);
txtRentals.frame = CGRectOffset(txtRentals.frame, 0, -highlightsPixelsRemoved);
}
//----------------------------------------------------------------------------
txtTents.text = _Campground.tents;
//----------------------------------------------------------------------------
txtNotes.text = _Campground.notes;
[self fitFrameToContent:txtNotes];
newTextViewHeight = txtNotes.contentSize.height;
if (newTextViewHeight > textViewDefaultHeight) {
notesPixelsAdded = newTextViewHeight - textViewDefaultHeight;
viewPixelsToBeAdded += notesPixelsAdded;
txtDirectionsHead.frame = CGRectOffset(txtDirectionsHead.frame, 0, notesPixelsAdded);
txtDirections.frame = CGRectOffset(txtDirections.frame, 0, notesPixelsAdded);
txtRentalsHead.frame = CGRectOffset(txtRentalsHead.frame, 0, notesPixelsAdded);
txtRentals.frame = CGRectOffset(txtRentals.frame, 0, notesPixelsAdded);
} else if (newTextViewHeight < textViewDefaultHeight) {
notesPixelsRemoved = textViewDefaultHeight - newTextViewHeight;
viewPixelsToBeRemoved += notesPixelsRemoved;
txtDirectionsHead.frame = CGRectOffset(txtDirectionsHead.frame, 0, -notesPixelsRemoved);
txtDirections.frame = CGRectOffset(txtDirections.frame, 0, -notesPixelsRemoved);
txtRentalsHead.frame = CGRectOffset(txtRentalsHead.frame, 0, -notesPixelsRemoved);
txtRentals.frame = CGRectOffset(txtRentals.frame, 0, -notesPixelsRemoved);
}
//----------------------------------------------------------------------------
txtDirections.text = _Campground.directions;
[self fitFrameToContent:txtDirections];
newTextViewHeight = txtDirections.contentSize.height;
if (newTextViewHeight > textViewDefaultHeight) {
directionsPixelsAdded = newTextViewHeight - textViewDefaultHeight;
viewPixelsToBeAdded += directionsPixelsAdded;
txtRentalsHead.frame = CGRectOffset(txtRentalsHead.frame, 0, directionsPixelsAdded);
txtRentals.frame = CGRectOffset(txtRentals.frame, 0, directionsPixelsAdded);
} else if (newTextViewHeight < textViewDefaultHeight) {
directionsPixelsRemoved = textViewDefaultHeight - newTextViewHeight;
viewPixelsToBeRemoved += directionsPixelsRemoved;
txtRentalsHead.frame = CGRectOffset(txtRentalsHead.frame, 0, -directionsPixelsRemoved);
txtRentals.frame = CGRectOffset(txtRentals.frame, 0, -directionsPixelsRemoved);
}
//----------------------------------------------------------------------------
txtRentals.text = _Campground.rentals;
[self fitFrameToContent:txtRentals];
newTextViewHeight = txtRentals.contentSize.height;
if (newTextViewHeight > textViewDefaultHeight) {
rentalsPixelsAdded = newTextViewHeight - textViewDefaultHeight;
viewPixelsToBeAdded += rentalsPixelsAdded;
} else if (newTextViewHeight < textViewDefaultHeight) {
rentalsPixelsRemoved = textViewDefaultHeight - newTextViewHeight;
viewPixelsToBeRemoved += rentalsPixelsRemoved;
}
viewHeight += viewPixelsToBeAdded;
viewHeight -= viewPixelsToBeRemoved;
detailsView.frame = CGRectMake(0, 20, 320, viewHeight);
scrollView.frame = CGRectMake(0, 20, 320, viewHeight);
}

It sounds like you're setting the same frame, which is much larger than the screen, on both the UIView and the UIScrollView? So the scroll view doesn't scroll because it's the same size as the view within it — there's nothing extra to show by scrolling.
Probably you want to leave the scroll view frame alone, so that the scroll view takes up the same amount of space on screen regardless of the size of its contents, and set the contentSize property. Similarly I'd expect you to want to pass 0 as the y coordinate for the view within the scrollview since view frames are relative to their parent.

A scrollView has two sizes: Its frame, which determines how large (and where) it is on the screen, and its contentSize, which determines how far it will scroll.
If contentSize is the same or smaller than frame.size, the scrollView will not scroll (because there is nothing to scroll to).
Usually, you'll want to set the contentSize of a scrollView to the size of the view(s) that you put into it.
Say you have a UIImageView that has a size of 1000x1000 points. Its frame will probably by CGRectMake(0, 0, 1000, 1000). Say your ScrollView is a fullscreen on an iPhone in portrait orientation, so it has a frame of CGRectMake(0, 0, 320, 480). If you now set its contentSize to CGSizeMake(1000, 1000), it will be able to scroll over all of the picture. If you make the contentSize a rectangle of 320 x 480, it won't scroll at all. And if you set the contentSize to 2000x2000, you will be able to scroll further than the picture.

Related

Customized Grid View with Different Items for Odd and Even Indexes

I have used this custom grid view in my code, and I want to create a grid view which looks like this:
How should I do this in the code? Here's the setup method for items:
- (void)setupItemViews {
for (UIView *view in self.itemViews) {
[view removeFromSuperview];
}
for (UIImageView *image in self.images) {
[image removeFromSuperview];
}
[self.images removeAllObjects];
[self.itemViews removeAllObjects];
// now add the new objects
NSUInteger numItems = [self.menuDelegate menuViewNumberOfItems:self];
for (NSUInteger i = 0; i < numItems; i++) {
GridMenuItemView *itemView = [[GridMenuItemView alloc] init];
GridMenuItem *menuItem = [self.menuDelegate menuView:self itemForIndex:i];
itemView.frame = CGRectMake(0, 0, self.itemSize.width, self.itemSize.height);
itemView.label.text = menuItem.title;
//itemView.imageView.image = menuItem.icon;
itemView.tag = i;
[itemView addTarget:self
action:#selector(itemPressed:)
forControlEvents:UIControlEventTouchUpInside];
[itemView setBackgroundImage:[UIImage imageNamed:menuItem.normalImageName]
forState:UIControlStateNormal];
[itemView setBackgroundImage:[UIImage imageNamed:menuItem.highlightedImageName]
forState:UIControlStateHighlighted];
NSUInteger numColumns = self.bounds.size.width > self.bounds.size.height ? self.columnCountLandscape : self.columnCountPortrait;
[self.itemViews addObject:itemView];
[self addSubview:itemView];
}
Thanks to ctrahey, I got what I wanted. But the method used for setting up the cells is -(void) layoutSubviews as follows:
You can find the corresponding code after this line: **//__n__ ...
- (void)layoutSubviews {
[super layoutSubviews];
//[self removeFromSuperview];
NSUInteger numColumns = self.bounds.size.width > self.bounds.size.height ? self.columnCountLandscape : self.columnCountPortrait;
NSUInteger numItems = [self.menuDelegate menuViewNumberOfItems:self];
if (self.itemViews.count != numItems) {
[self setupItemViews];
}
CGFloat padding = roundf((self.bounds.size.width - (self.itemSize.width * numColumns)) / (numColumns + 1));
NSUInteger numRows = numItems % numColumns == 0 ? (numItems / numColumns) : (numItems / numColumns) + 1;
CGFloat yPadding = 0;
CGFloat totalHeight = ((self.itemSize.height + yPadding) * numRows) + yPadding ;
// get an even y padding if less than the max number of rows
if (totalHeight < self.bounds.size.height) {
CGFloat leftoverHeight = self.bounds.size.height - totalHeight;
CGFloat extraYPadding = roundf(leftoverHeight / (numRows + 1));
yPadding += extraYPadding;
totalHeight = ((self.itemSize.height + yPadding) * numRows) + yPadding;
}
// get an even x padding if we have less than a single row of items
if (numRows == 1 && numItems < numColumns) {
padding = roundf((self.bounds.size.width - (numItems * self.itemSize.width)) / (numItems + 1));
}
for (int i = 0, j = numColumns; i < numItems; i++) {
j--;
if (j<0) {
j += numColumns;
}
//NSUInteger column = j ;
//NSUInteger row = i / numColumns;
UIView *item = [self.itemViews objectAtIndex:i];
//CGFloat xOffset = (column * (self.itemSize.width + padding)) + padding;
//CGFloat yOffset = (row * (self.itemSize.height + yPadding));// + yPadding;
**//__n__ These three lines of code make set the cell items at different xPositions from right and left.
CGFloat xPosition = (i%2) ? 0.0 : self.bounds.size.width - self.itemSize.width;
CGFloat yPosition = i*self.itemSize.height;
item.frame = CGRectMake(xPosition, yPosition, self.itemSize.width, self.itemSize.height);**
//item.frame = CGRectMake(xOffset, yOffset, self.itemSize.width, self.itemSize.height);
// if ((i%numColumns) == 0) {
// UIImageView *img = [self.images objectAtIndex:i/numColumns];
// img.frame = CGRectMake(0, yOffset+76, self.bounds.size.width, 1);
// }//End If
}
//Scroll size.
UIDeviceOrientation currentOrientation = [[UIDevice currentDevice] orientation];
if (UIDeviceOrientationIsPortrait(currentOrientation))
{
self.contentSize = CGSizeMake(self.bounds.size.width, totalHeight + (yPadding * (numRows+1.55)));
}
else if (UIDeviceOrientationIsLandscape(currentOrientation))
{
self.contentSize = CGSizeMake(self.bounds.size.width, totalHeight + (yPadding * (numRows+3)));
}
}
If I understand your intention correctly, the line:
itemView.frame = CGRectMake(0, 0, self.itemSize.width, self.itemSize.height);
Should be using your index i and some simple arithmetic to position the views. One technique I have used in the past is a simple is-odd check using the modulus operator.
CGFloat xPosition = (i%2) ? 0.0 : self.bounds.size.width - self.itemSize.width;
CGFloat yPosition = i*self.itemSize.height;
itemView.frame = CGRectMake(xPosition, yPosition, self.itemSize.width, self.itemSize.height);
just use the uitableview operation.
in that cellforrowatindexpath
set button image and frame based on the odd and even
if(indexpath.orw%2==0)
{
//set your frame here;
}
else
{
// set your frame here;
}
Thanks to ctrahey. The method used for setting up the cells is -(void) layoutSubviews as follows:
You can find the corresponding code after this line: **//__n__ ...
- (void)layoutSubviews {
[super layoutSubviews];
//[self removeFromSuperview];
NSUInteger numColumns = self.bounds.size.width > self.bounds.size.height ? self.columnCountLandscape : self.columnCountPortrait;
NSUInteger numItems = [self.menuDelegate menuViewNumberOfItems:self];
if (self.itemViews.count != numItems) {
[self setupItemViews];
}
CGFloat padding = roundf((self.bounds.size.width - (self.itemSize.width * numColumns)) / (numColumns + 1));
NSUInteger numRows = numItems % numColumns == 0 ? (numItems / numColumns) : (numItems / numColumns) + 1;
CGFloat yPadding = 0;
CGFloat totalHeight = ((self.itemSize.height + yPadding) * numRows) + yPadding ;
// get an even y padding if less than the max number of rows
if (totalHeight < self.bounds.size.height) {
CGFloat leftoverHeight = self.bounds.size.height - totalHeight;
CGFloat extraYPadding = roundf(leftoverHeight / (numRows + 1));
yPadding += extraYPadding;
totalHeight = ((self.itemSize.height + yPadding) * numRows) + yPadding;
}
// get an even x padding if we have less than a single row of items
if (numRows == 1 && numItems < numColumns) {
padding = roundf((self.bounds.size.width - (numItems * self.itemSize.width)) / (numItems + 1));
}
for (int i = 0, j = numColumns; i < numItems; i++) {
j--;
if (j<0) {
j += numColumns;
}
//NSUInteger column = j ;
//NSUInteger row = i / numColumns;
UIView *item = [self.itemViews objectAtIndex:i];
//CGFloat xOffset = (column * (self.itemSize.width + padding)) + padding;
//CGFloat yOffset = (row * (self.itemSize.height + yPadding));// + yPadding;
**//__n__ These three lines of code make set the cell items at different xPositions from right and left.
CGFloat xPosition = (i%2) ? 0.0 : self.bounds.size.width - self.itemSize.width;
CGFloat yPosition = i*self.itemSize.height;
item.frame = CGRectMake(xPosition, yPosition, self.itemSize.width, self.itemSize.height);**
//item.frame = CGRectMake(xOffset, yOffset, self.itemSize.width, self.itemSize.height);
// if ((i%numColumns) == 0) {
// UIImageView *img = [self.images objectAtIndex:i/numColumns];
// img.frame = CGRectMake(0, yOffset+76, self.bounds.size.width, 1);
// }//End If
}
//Scroll size.
UIDeviceOrientation currentOrientation = [[UIDevice currentDevice] orientation];
if (UIDeviceOrientationIsPortrait(currentOrientation))
{
self.contentSize = CGSizeMake(self.bounds.size.width, totalHeight + (yPadding * (numRows+1.55)));
}
else if (UIDeviceOrientationIsLandscape(currentOrientation))
{
self.contentSize = CGSizeMake(self.bounds.size.width, totalHeight + (yPadding * (numRows+3)));
}
}

Show multiple photos in scroller

In the app, I need to show no. of images (count- unknown) in scroller. I put all the images in scroller directly. But start receiving the memory warning level 2. Please suggest me how to do so. Is there any other way to do that?
You can also refer to following link to have better understanding of the problem.
https://www.dropbox.com/sh/h0vwnhhx1acfcb5/W2TQ638udh
-(void) getPortraitScrollReady
{
UIImageView *imageView;
NSString *imageName;
int x=0;
for (NSNumber *numberObject in tagList)
{
imageName = [[NSString alloc] initWithFormat: #"%#", [self getImage:[numberObject intValue]]];
imageView = [[UIImageView alloc ] initWithFrame: CGRectMake(x, 0, 320, 320)];
imageView.image = [self thumbWithSideOfLength:320 pathForMain:imageName]; // a function that returns an image;
[scrollPortrait addSubview:imageView];
[imageName release];
[imageView release];
x+=320;
}
scrollPortrait.contentSize = CGSizeMake(x, 320);
int pageNo =[self indexofObject:imageNo inArray:tagList];
[scrollPortrait setContentOffset:CGPointMake(pageNo*320, 0) animated:NO];
}
Keep all images in an Array/MutableArray as per convenience.
Take Scrollview, In scroll view take 5-UIImageViews to display images from array. Match the length of that UIScrollView with 5-UIImageViews.
When scrolling is done in any of the direction, then 'release' the UIImageView in opposite direction & 'allocate' a new UIImageVIew in scrolled direction.
This is what I did in my project.
.h file contents
IBOutlet UIScrollView *scrlView;
UIImageView *imgView1;
UIImageView *imgView2;
UIImageView *imgView3;
UIImageView *imgView4;
UIImageView *imgView5;
NSMutableArray *imgGallery;
NSInteger mCount;
NSInteger centerImg;
CGFloat imgFrameHeight;
CGFloat imgView1X;
CGFloat imgView2X;
CGFloat imgView3X;
CGFloat imgView4X;
CGFloat imgView5X;
CGFloat previousOffsetX;
CGFloat currentOffsetX;
.m file contents
- (void)photoGallery
{
imgGallery = [[NSMutableArray alloc] initWithCapacity:10];
[imgGallery addObject:[UIImage imageNamed:#"1.png"]];
[imgGallery addObject:[UIImage imageNamed:#"2.png"]];
[imgGallery addObject:[UIImage imageNamed:#"3.png"]];
[imgGallery addObject:[UIImage imageNamed:#"4.png"]];
[imgGallery addObject:[UIImage imageNamed:#"5.png"]];
[imgGallery addObject:[UIImage imageNamed:#"6.png"]];
[imgGallery addObject:[UIImage imageNamed:#"7.png"]];
[imgGallery addObject:[UIImage imageNamed:#"8.png"]];
[imgGallery addObject:[UIImage imageNamed:#"9.png"]];
}
- (void)photoScroller
{
CGFloat appFrameHeight = self.view.frame.size.height;
CGFloat navFrameHeight = self.navigationController.navigationBar.frame.size.height;
CGFloat tabFrameHeight = self.tabBarController.tabBar.frame.size.height;
imgFrameHeight = appFrameHeight - (navFrameHeight+tabFrameHeight);
mCount = [imgGallery count];
[scrlView setContentSize:CGSizeMake(320 * mCount, imgFrameHeight)];
CGFloat offsetX = ((320 * mCount)/2) - 160;
[scrlView setContentOffset:CGPointMake(offsetX, 0)];
imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(offsetX - (320*2), 0, 320, imgFrameHeight)];
imgView2 = [[UIImageView alloc] initWithFrame:CGRectMake(offsetX - 320, 0, 320, imgFrameHeight)];
imgView3 = [[UIImageView alloc] initWithFrame:CGRectMake(offsetX, 0, 320, imgFrameHeight)];
imgView4 = [[UIImageView alloc] initWithFrame:CGRectMake(offsetX + 320, 0, 320, imgFrameHeight)];
imgView5 = [[UIImageView alloc] initWithFrame:CGRectMake(offsetX + (320*2), 0, 320, imgFrameHeight)];
int center = mCount/2;
int tmp = center * 2;
int remainder = mCount - tmp;
if (remainder != 0) {
centerImg = center;
} else {
centerImg = center - remainder;
}
imgView1.image = [imgGallery objectAtIndex:(centerImg-2)];
imgView2.image = [imgGallery objectAtIndex:(centerImg-1)];
imgView3.image = [imgGallery objectAtIndex:centerImg];
imgView4.image = [imgGallery objectAtIndex:(centerImg+1)];
imgView5.image = [imgGallery objectAtIndex:(centerImg+2)];
[scrlView addSubview:imgView1];
[scrlView addSubview:imgView2];
[scrlView addSubview:imgView3];
[scrlView addSubview:imgView4];
[scrlView addSubview:imgView5];
}
Below is the code where UIImageViews are adjusted
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
imgView1X = imgView1.frame.origin.x;
imgView2X = imgView2.frame.origin.x;
imgView3X = imgView3.frame.origin.x;
imgView4X = imgView4.frame.origin.x;
imgView5X = imgView5.frame.origin.x;
CGPoint previousOffset = [scrlView contentOffset];
previousOffsetX = previousOffset.x;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//get the current offset
CGPoint currentOffset = [scrollView contentOffset];
currentOffsetX = currentOffset.x;
//NSLog(NSStringFromCGPoint(currentOffset));
//calculate offset X of RIGHT ImageView to be shifted
CGFloat identifyRightIV = previousOffsetX +640;
//calcualte new LEFT offset X for shifting the previously calcualted RIGHT ImageView
CGFloat newLX = identifyRightIV - (320*5);
//calculate offset X of LEFT ImageView to be shifted
CGFloat identifyLeftIV = previousOffsetX -640;
//calcualte new RIGHT offset X for shifting the previously calcualted LEFT ImageView
CGFloat newRX = identifyLeftIV + (320*5);
//index of new LEFT Image in an array imgGallery
int newImageL = newLX / 320;
//index of new RIGHT Image in an array imgGallery
int newImageR = newRX / 320;
//if scrolled to left
if (newLX >= 0) // Is shifting is necessary? i.e. if new LEFT offsetX is greater than lowest offsetX(0) of scrollview
{
if (currentOffsetX == (previousOffsetX-320))
{
if (imgView1X == identifyRightIV) {
imgView1.frame = CGRectMake(newLX, 0, 320, imgFrameHeight);
imgView1.image = [imgGallery objectAtIndex:newImageL];
}
else if (imgView2X == identifyRightIV) {
imgView2.frame = CGRectMake(newLX, 0, 320, imgFrameHeight);
imgView2.image = [imgGallery objectAtIndex:newImageL];
}
else if (imgView3X == identifyRightIV) {
imgView3.frame = CGRectMake(newLX, 0, 320, imgFrameHeight);
imgView3.image = [imgGallery objectAtIndex:newImageL];
}
else if (imgView4X == identifyRightIV) {
imgView4.frame = CGRectMake(newLX, 0, 320, imgFrameHeight);
imgView4.image = [imgGallery objectAtIndex:newImageL];
}
else if (imgView5X == identifyRightIV) {
imgView5.frame = CGRectMake(newLX, 0, 320, imgFrameHeight);
imgView5.image = [imgGallery objectAtIndex:newImageL];
}
}
}
//if scrolled to right
if (newRX < (mCount*320)) // Is shifting is necessary? i.e. if new RIGHT offsetX is less than highest offsetX of scrollview
{
if (currentOffsetX == (previousOffsetX+320))
{
if (imgView1X == identifyLeftIV) {
imgView1.frame = CGRectMake(newRX, 0, 320, imgFrameHeight);
imgView1.image = [imgGallery objectAtIndex:newImageR];
}
else if (imgView2X == identifyLeftIV) {
imgView2.frame = CGRectMake(newRX, 0, 320, imgFrameHeight);
imgView2.image = [imgGallery objectAtIndex:newImageR];
}
else if (imgView3X == identifyLeftIV) {
imgView3.frame = CGRectMake(newRX, 0, 320, imgFrameHeight);
imgView3.image = [imgGallery objectAtIndex:newImageR];
}
else if (imgView4X == identifyLeftIV) {
imgView4.frame = CGRectMake(newRX, 0, 320, imgFrameHeight);
imgView4.image = [imgGallery objectAtIndex:newImageR];
}
else if (imgView5X == identifyLeftIV) {
imgView5.frame = CGRectMake(newRX, 0, 320, imgFrameHeight);
imgView5.image = [imgGallery objectAtIndex:newImageR];
}
}
}
}
Due to this in entire life cycle only 5-UIImageViews will be in App & all images will be safe in array.
try this code for 1st question:
scrl=[[UIScrollView alloc]initWithFrame:CGRectMake(10, 92, 300, 370)];
//scrl.backgroundColor=[UIColor blackColor];
scrl.delegate=self;
scrl.showsHorizontalScrollIndicator=NO;
scrl.showsVerticalScrollIndicator=NO;
scrl.pagingEnabled=YES;
scrl.layer.cornerRadius = 5.0f;
scrl.layer.masksToBounds = YES;
scrl.layer.borderWidth = 5;
scrl.layer.borderColor = [UIColor blackColor].CGColor;
int i, x=0,y=0;
for (i=0; i<[imageName count]; i++)
{
imagBack=[[UIImageView alloc] initWithFrame:CGRectMake(x,y,300, 370)];
imagBack.image=[UIImage imageNamed:[imageName objectAtIndex:i]];
[scrl addSubview:imagBack];
x =x+300;
}
scrl.contentSize=CGSizeMake(x,370);
[self.view addSubview:scrl];
-> 2nd issue,you use the image to be compressed (.png format) image is ,try this:
[self imageWithImage:image CovertToSize:CGSizeMake(46, 44)];
NSData* imageData = UIImageJPEGRepresentation(image, 1.0f);//10 % quality compressed
call this method when save the image:
-(UIImage *)imageWithImage:(UIImage *)image CovertToSize:(CGSize)size {
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
destImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return destImage;
}

how to add subviews in a 'for' loop

I'm simply trying to add a UIView for every object in an array without displaying more than 3 on screen but the views aren't next to each other. There is a big gap (one view width) between each view. Here's what I've got;
int numberOfUsersOnScreen;
if (array.count < 3) {
numberOfViewsOnScreen = array.count;
}else{
numberOfUsersOnScreen = 3;
}
double width = (self.scrollView.frame.size.width/numberOfViewsOnScreen);
CGRect r = CGRectMake(0, 0, width, 1200);
[self.usersScrollView setContentSize:CGSizeMake(width*array.count, 0)];
for (int i = 0; i < users.count; i++) {
r.origin.x = width * i;
UIView * view = [[UIView alloc] initFrame:r];
[self.scrollView addSubview:view];
}
Try this:
int xPosition = 0;
for (int i = 0; i < users.count; i++) {
UIView * view = [[UIView alloc] initFrame:CGRectMake(xPosition, 0, width, 1200)];
[self.scrollView addSubview:view];
xPosition += width;
}
Instead of
[self.scrollView setContentSize:CGSizeMake(width*array.count, 0)];
It should be
[self.scrollView setContentSize:CGSizeMake(width*array.count, self.scrollView.frame.size.height)];
for (int i = 0; i < users.count; i++) {
r.origin.x = width * i;
UIView * view = [[UIView alloc] initFrame:r];
[self.scrollView addSubview:view];
}
In this way you have also a memory leak on view(s) object
for (int i = 0; i < users.count; i++) {
r.origin.x = width * i;
UIView * view = [[UIView alloc] initFrame:r];
[self.scrollView addSubview:view];
[view release];
}

Animate multiple UIButtons

Hi I am beginner to iOS and I have a question.
I want to re arrange multiple buttons when changing orientations so i have this code to create them :
NSArray *buttonImages = [[NSArray alloc] initWithObjects:#"button_1.png",#"button_2.png",
#"button_3.png", #"button_4.png", nil];
int xcord = 0;
//int ycord = 0;
UIInterfaceOrientation destOrientation = self.interfaceOrientation;
if (destOrientation == UIInterfaceOrientationPortrait || destOrientation == UIInterfaceOrientationPortraitUpsideDown) {
screenWidth = 768;
} else {
screenWidth = 1024;
}
int buttonWidth = 57;
int buttonHeight = 86;
int screenHorizontalDivisions = screenWidth / 5;
for (int i=0; i<4; i++) {
int buttonStartingOffset = screenHorizontalDivisions - buttonWidth / 2;
btn = [[DashButton alloc] initWithFrame:CGRectMake(buttonStartingOffset + xcord, 40, buttonWidth,buttonHeight) andImage:[buttonImages objectAtIndex:i] andTitle:#"Test"];
btn.tag = i+1;
[springboardScrollView addSubview:btn];
//[btn release];
xcord = xcord + screenHorizontalDivisions;
}
and the code to re arrange them is:
- (void)animate: (id)sender {
UIButton *button = (UIButton *)sender;
UIInterfaceOrientation destOrientation = self.interfaceOrientation;
if (destOrientation == UIInterfaceOrientationPortrait || destOrientation == UIInterfaceOrientationPortraitUpsideDown) {
screenWidth = 768;
} else {
screenWidth = 1024;
}
int xcord = 0;
int buttonWidth = 57;
int buttonHeight = 86;
int screenHorizontalDivisions = screenWidth / 5; //find the divisions
int buttonStartingOffset = screenHorizontalDivisions - buttonWidth / 2; //center the button
NSLog (#"%i", button.tag);
switch (button.tag) {
case 1:
button.frame = CGRectMake(buttonStartingOffset, 40, buttonWidth,buttonHeight);
break;
case 2:
button.frame = CGRectMake(buttonStartingOffset + screenHorizontalDivisions, 40, buttonWidth,buttonHeight);
break;
case 3:
button.frame = CGRectMake(buttonStartingOffset + screenHorizontalDivisions * 2, 40, buttonWidth,buttonHeight);
break;
case 4:
button.frame = CGRectMake(buttonStartingOffset + screenHorizontalDivisions * 3, 40, buttonWidth,buttonHeight);
break;
}
then I am calling it when orientation changes:
- (void)willAnimateSecondHalfOfRotationFromInterfaceOrientation: (UIInterfaceOrientation) fromInterfaceOrientation duration: (NSTimeInterval) duration {
[self animate:btn];
How can I refer to each button one by one?
The code in animate: always return button.tag=4.
Please assist!!
Thanks
Bill.
You need to get the array of UIButtons with the following code:
NSArray* buttons = [springboardScrollView subviews]
This will return all the subviews in the scrollView. If you have UiButtons only that will be all buttons in the scroll view. If you have other types of views in scroll view then you need to iterate through array and check if the object is UiButton, which is explained on the following link .
Then you can change the frame, image, position....

UIScrollView offsetting content double the requested amount

The Issue
My PSWSnapshotView's end up exactly snapshotIndex too far to the right inside the _scrollView (40px offset). Yet, they are set with their frame to have the first one at (0, 0), though that first one shows up at (snapshotInset, 0) instead :/.
The Code
- (void)layoutViews
{
CGRect frame = self.frame;
CGFloat pageWidth = frame.size.width - (snapshotInset * 2);
CGRect pageFrame = CGRectMake(0.0f, 0.0f, pageWidth, frame.size.height);
NSInteger pageCount = [applications count];
[pageControl setNumberOfPages:pageCount];
[scrollView setFrame:CGRectMake(snapshotInset, 0.0f, pageWidth, frame.size.height)];
[scrollView setContentSize:CGSizeMake((pageWidth * pageCount) + 1.0f, frame.size.height)];
for (int i = 0; i < pageCount; i++) {
PSWSnapshotView *view;
if ([snapshotViews count] <= i)
{
PSWSnapshotView *view = [[PSWSnapshotView alloc] initWithFrame:pageFrame application:[applications objectAtIndex:i]];
view.delegate = self;
[scrollView addSubview:view];
[snapshotViews addObject:view];
[view release];
}
else
{
view = [snapshotViews objectAtIndex:i];
[view setFrame:pageFrame];
}
pageFrame.origin.x += pageWidth;
}
}
The Values
self.frame.size.width = 320;
self.frame.size.height = 370;
snapshotInset = 40;
[applications count] = 2;
All the sizes and frames are set correctly (I checked w/ NSLog).
Are you possibly setting the scrollView.contentOffset somewhere, independent of this? Also, and this may just be a copy/paste issue, you reference both snapshotInset and _snapshotInset.