My scenario is when scrollview moving horizontally it has to show one question with table view and with some controls inside table view..
When moving scrollview horizontally i am releasing previous tables and labels and creating new tables and new labels.
But my app is crashing after scrolling some 20 times i.e., in device only but working fine in simulator.
I run app with instruments there are no leaks..
My doubt is that is there any problem with this iPAD 1st generation RAM means not sufficient or any other problem..
Here is my code..
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
#try {
cancelScroll = NO;
if (scrollEnabled) {
scrollEnabled = NO;
NSLog(#"Begin Scroll Dragging");
NSLog(#"%f -- %f",scrollView.contentOffset.x,scrollView.contentOffset.y);
if (scrollDirection == 1) {
scrollDirection = 0;
rect = mainTable.frame;
[scrollTableView scrollRectToVisible:rect animated:NO];
return;
}
else if (scrollDirection == 2) {
scrollDirection = 0;
HUDProgress.alpha=0;
pageWidth = scrollTableView.frame.size.width;
if (pageControlBeingUsed) {
page = pageControl.currentPage;
}
else {
page= floor((scrollTableView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
}
NSLog(#"%d",page);
if (page == [questionTextArray count] - 1) {
if (maxValueReached) {
rect = mainTable.frame;
[scrollTableView scrollRectToVisible:rect animated:NO];
return;
}
}
photoAlbumView.alpha = 0;
photoView.alpha = 0;
FileAddView.alpha = 0;
settingsView.alpha = 0;
refreshView.alpha = 0;
photoAlbumViewPortrait.alpha = 0;
FileAddViewPortrait.alpha = 0;
settingsViewPortrait.alpha = 0;
refreshViewPortrait.alpha = 0;
checkValueForTableView = TRUE;
if (orientation == 1 ) {
if (!expandAction) {
scrollTableView.contentSize = CGSizeMake([questionTextArray count] * 720.0, 596.0);
}
else {
scrollTableView.contentSize = CGSizeMake([questionTextArray count] * 930.0, 596.0);
}
}
else {
if (!expandAction) {
scrollTableView.contentSize = CGSizeMake([questionTextArray count] * 520.0, 680.0);
}
else {
scrollTableView.contentSize = CGSizeMake([questionTextArray count] * 720.0, 680.0);
}
}
if (page >= 0) {
if (page > tempPage) {
difference = page - tempPage;
NSLog(#"%d",difference);
NSLog(#"%f",tableXPosition);
tableXPosition = (tableXPosition + (difference * tableWidthPosition));
NSLog(#"%f",tableXPosition);
}
else if (page < tempPage){
difference = tempPage - page;
NSLog(#"%d",difference);
tableXPosition = abs ((difference * tableWidthPosition) - tableXPosition);
NSLog(#"%f",tableXPosition);
}
else {
rect = mainTable.frame;
[scrollTableView scrollRectToVisible:rect animated:NO];
}
}
if([questionTextArray count] > 1)
{
pageControl.numberOfPages = [questionTextArray count];
[pageControl addTarget:self action:#selector(changePage) forControlEvents:UIControlEventValueChanged];
if(scrollTableView != nil)
{
if (mainTable != nil) {
// mainTable.delegate = nil;
[mainTable release];
mainTable = [[UITableView alloc] initWithFrame:CGRectMake(tableXPosition , 45.0, tableWidthPosition, 565.0) style:UITableViewStyleGrouped];
}
mainTable.delegate = self;
mainTable.dataSource = self;
mainTable.scrollEnabled = YES;
mainTable.backgroundColor = [UIColor clearColor];
[mainTable reloadData];
[scrollTableView addSubview:mainTable];
}
NSLog(#"%d %d",page,previousPage);
if (page >= 0) {
if (previousPage != page) {
pageControl.currentPage= page;
}
}
if (page >= 0) {
if (previousPage != page) {
UIImageView *imgView = [[UIImageView alloc] init];
UILabel *questionTextLbl1 = [[UILabel alloc] init];
UILabel *questionNumberLbl1 = [[UILabel alloc] init];
if (orientation == 1) {
if (!expandAction) {
imgView.frame = CGRectMake(tableXPosition, 0.0, 710.0, 39.0);
questionTextLbl1.frame = CGRectMake(10.0, 2.0, 430.0, 35.0);
questionNumberLbl1.frame = CGRectMake(550.0, 2.0, 200.0, 35.0);
}
else {
imgView.frame = CGRectMake(tableXPosition, 0.0, 920.0, 39.0);
questionTextLbl1.frame = CGRectMake(50.0, 2.0, 500.0, 35.0);
questionNumberLbl1.frame = CGRectMake(800.0, 2.0, 150.0, 35.0);
compressButton = [UIButton buttonWithType:UIButtonTypeCustom];
compressButton.frame = CGRectMake(10.0, 10.0, 25.0, 25.0);
[compressButton setImage:[UIImage imageNamed:#"Drop Down small2.png"] forState:UIControlStateNormal];
[compressButton addTarget:self action:#selector(compressAction) forControlEvents:UIControlEventTouchUpInside];
}
imgView.contentMode = UIViewContentModeScaleToFill;
imgView.backgroundColor = [UIColor clearColor];
imgView.image = [UIImage imageNamed:#"Top gary header.png"];
[scrollTableView addSubview:imgView];
if (!expandAction) {
NSLog(#"expandAction");
}
else {
compressButton.backgroundColor = [UIColor clearColor];
[compressButton addTarget:self action:#selector(compressAction) forControlEvents:UIControlEventTouchUpInside];
[imgView addSubview:compressButton];
imgView.userInteractionEnabled = YES;
}
questionTextLbl1.backgroundColor = [UIColor clearColor];
questionTextLbl1.numberOfLines = 0;
questionTextLbl1.text = [NSString stringWithFormat:#"%d.%#?",page + 1,[questionTextArray objectAtIndex:page]];
NSLog(#"Question arry %#", questionTextArray);
[imgView addSubview:questionTextLbl1];
if([questionTextArray count] > 1)
{
questionNumberLbl1.text = [NSString stringWithFormat:#"Question %d/%d",page + 1,[questionTextArray count]];
}
else {
questionNumberLbl1.text = [NSString stringWithFormat:#"Question 1"];
}
questionNumberLbl1.backgroundColor = [UIColor clearColor];
questionNumberLbl1.numberOfLines = 0;
[imgView addSubview:questionNumberLbl1];
}
else {
if (!expandAction) {
imgView.frame = CGRectMake(tableXPosition, 0.0, 530.0, 38.0);
questionTextLbl1.frame = CGRectMake(10.0, 2.0, 350.0, 35.0);
questionNumberLbl1.frame = CGRectMake(380.0, 2.0, 150.0, 35.0);
}
else {
imgView.frame = CGRectMake(tableXPosition, 0.0, 720.0, 38.0);
questionTextLbl1.frame = CGRectMake(40.0, 2.0, 500.0, 35.0);
questionNumberLbl1.frame = CGRectMake(550.0, 2.0, 150.0, 35.0);
compressButton = [UIButton buttonWithType:UIButtonTypeCustom];
compressButton.frame = CGRectMake(10.0, 10.0, 25.0, 25.0);
[compressButton setImage:[UIImage imageNamed:#"Drop Down small2.png"] forState:UIControlStateNormal];
[compressButton addTarget:self action:#selector(compressAction) forControlEvents:UIControlEventTouchUpInside];
}
imgView.contentMode = UIViewContentModeScaleToFill;
imgView.backgroundColor = [UIColor clearColor];
imgView.image = [UIImage imageNamed:#"Top gary header.png"];
[scrollTableView addSubview:imgView];
if (!expandAction) {
NSLog(#"expandAction");
}
else {
[imgView addSubview:compressButton];
imgView.userInteractionEnabled = YES;
}
questionTextLbl1.backgroundColor = [UIColor clearColor];
questionTextLbl1.numberOfLines = 0;
questionTextLbl1.font = [UIFont systemFontOfSize:14.0];
questionTextLbl1.text = [NSString stringWithFormat:#"%d.%#?",page + 1,[questionTextArray objectAtIndex:page]];
NSLog(#"Question arry %#", questionTextArray);
[imgView addSubview:questionTextLbl1];
if([questionTextArray count] > 1)
{
questionNumberLbl1.text = [NSString stringWithFormat:#"Question %d/%d",page + 1,[questionTextArray count]];
}
else {
questionNumberLbl1.text = [NSString stringWithFormat:#"Question 1"];
}
questionNumberLbl1.backgroundColor = [UIColor clearColor];
questionNumberLbl1.numberOfLines = 0;
[imgView addSubview:questionNumberLbl1];
}
[mainTable reloadData];
// [scrollTableView addSubview:mainTable];
rect = mainTable.frame;
if (pageControlBeingUsed) {
[scrollTableView scrollRectToVisible:rect animated:YES];
pageControlBeingUsed = NO;
}
else {
[scrollTableView scrollRectToVisible:rect animated:NO];
}
tempPage = page;
previousPage = page;
if (page == [questionTextArray count] - 1){
maxValueReached = YES;
}
else {
maxValueReached = NO;
}
[imgView release];
[questionNumberLbl1 release];
[questionTextLbl1 release];
}
}
NSLog(#"page:%d previous page:%d temp page:%d",page,previousPage,tempPage);
}
}
}
else {
[self killScroll];
scrollDirection = 0;
}
}
#catch (NSException *exception) {
NSLog(#"Exception: %#---%#",[exception name],[exception reason]);
}
#finally {
NSLog(#"Finally block");
}
}
My doubt is that is there any problem with this iPAD 1st generation RAM means not sufficient or any other problem.
Your intuition about memory is correct, IMO. Your method is really hard to follow, so I am not sure but it seems that you are instantiating a log of views and label and adding them to your main scrollTableView, but never removing them. That could explain the memory fill-up.
Take imgView, you instantiate it:
UIImageView *imgView = [[UIImageView alloc] init];
add it to scrollTableView:
[scrollTableView addSubview:imgView];
add several subviews to imgView:
[imgView addSubview:questionTextLbl1];
[imgView addSubview:questionNumberLbl1];
you release it correctly:
[imgView release];
but I cannot see any place where you remove imgView from scrollTableView before adding a new imgView to it... so you just keep adding imgView on top of imgView...
A simple way to check whether what I am saying is true is adding this statement to scrollViewDidEndDecelerating:
NSLog(#"LOGGING NUMBER OF SUBVIEWS: %d", [scrollTableView.subviews count]);
e.g.,
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
#try {
cancelScroll = NO;
if (scrollEnabled) {
NSLog(#"LOGGING NUMBER OF SUBVIEWS: %d", [scrollTableView.subviews count]);
...
and you will se that each time that you scroll, the number of subviews will increase.
Related
I am updating one app to iOS 6 to iOS 7. But I am getting issue regarding kind of class. In iOS 6 it is showing class name custom button but in iOS 7 it displays _UITextContainerView.
for (UIView *subView in textViewButton.subviews)
{
NSLog(#"yourObject is a: %#", [subView class]);
#autoreleasepool
{
if([subView isKindOfClass:[CustomButton class]])
{
CustomButton *button = (CustomButton*)subView;
button.backgroundColor = [UIColor redColor];
[button setType:kButtonTypeQuestion];
button.titleLabel.font = kFontForContentPhone;
if (button.tag == 62254 || button.tag == 62263)
{
CGRect tempFrame = button.frame;
tempFrame.origin.x = button.frame.origin.x - 3.0f;
button.frame = tempFrame;
}
if (self.soundFile != nil)
{
CGRect tempFrame = button.frame;
tempFrame.size.width = button.frame.size.width + 28.0f;
button.frame = tempFrame;
[button setContentEdgeInsets:UIEdgeInsetsMake(5.0f, 30.0f, 5.0f, 10.0f)];
}
}
}
}
Update
pragma mark - Text Paragraph iPhone
self.questionScrollView = [[CustomScrollView alloc] initWithFrame:CGRectMake(0, currentYPosition, self.frame.size.width, self.frame.size.height - currentYPosition - 60.0f)];
self.questionScrollView.showsVerticalScrollIndicator = NO;
self.questionsContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.questionScrollView.frame.size.width, self.questionScrollView.frame.size.height)];
currentYPosition = 0;
for (Question *question in self.questions)
{
#autoreleasepool
{
self.ttmItem = [[TapToMoveItem alloc] initWithFrame:CGRectMake(0, currentYPosition, self.frame.size.width, self.frame.size.height)];
self.ttmItem.ttmDelegate = self;
self.ttmItem.variant = self.exercise.variant;
ttmItem.x_Position = question.x_position;
ttmItem.y_Position = question.y_position;
if ([question.sound_file length] > 0 || question.sound_file != nil)
self.ttmItem.hasAudio = YES;
else
self.ttmItem.hasAudio = NO;
if ([kPrefixImage isEqualToString:#"preint_"] && ([self.exercise.exercise_id integerValue] == 54 || [self.exercise.exercise_id integerValue] == 91)) {
ttmItem.soundFile = self.exercise.header_title;
}
self.ttmItem.prefix = question.prefix;
self.ttmItem.text = question.text;
self.ttmItem.longestPrefix = longestPrefix;
self.ttmItem.longestAnswer = longestAnswer;
self.ttmItem.buttonWidth = sizeForButton.width;
//get and set answers
ttmItem.answers = [self.answers objectForKey:question.question_id];
[self.ttmItem createForPhone];
[self.questionsContainer addSubview:self.ttmItem];
currentYPosition += ttmItem.frame.size.height + SPACE_BET_VIEWS;
// itemHeight = ttmItem.frame.size.height; //hack
// itemWidth = ttmItem.frame.size.width;
ttmCount++;
if ([ttmItem.arrayOfTextViewButtons count] > 0) {
[dictionaryOfItemButtons setObject:ttmItem.arrayOfTextViewButtons forKey:question.question_id];
}
}
}
self.questionsContainer.frame = CGRectMake(questionsContainer.frame.origin.x, questionsContainer.frame.origin.y, questionsContainer.frame.size.width, currentYPosition );
[self.questionScrollView setContentSize:self.questionsContainer.frame.size];
[self.questionScrollView addSubview:self.questionsContainer];
[self addSubview:self.questionScrollView];
[self setScrollEnabled:NO];
}
I am unable to find that why it is showing different in class name of any object. Moreover I didn't find any issue regarding this on google. Please help me if someone has an idea about this.
Thanks in advance.
I have resolved this issue by doing this :
for (UIView *subView in textViewButton.textInputView.subviews)
{
// const char* className = class_getName([yourObject class]);
NSLog(#"yourObject is a: %#", [subView class]);
#autoreleasepool
{
if([subView isKindOfClass:[CustomButton class]])
{
CustomButton *button = (CustomButton*)subView;
button.backgroundColor = [UIColor clearColor];
[button setType:kButtonTypeQuestion];
button.titleLabel.font = kFontForContentPhone;
if (button.tag == 62254 || button.tag == 62263)
{
CGRect tempFrame = button.frame;
tempFrame.origin.x = button.frame.origin.x - 3.0f;
button.frame = tempFrame;
}
if (self.soundFile != nil)
{
CGRect tempFrame = button.frame;
tempFrame.size.width = button.frame.size.width + 28.0f;
button.frame = tempFrame;
[button setContentEdgeInsets:UIEdgeInsetsMake(5.0f, 30.0f, 5.0f, 10.0f)];
}
}
}
}
Loop through all the subviews as well until you found your object. if you happen to find the object exit the loop to save any unnecessary resources
for (UIView *subView in textViewButton.subviews)
{
NSLog(#"yourObject is a: %#", [subView class]);
#autoreleasepool
{
if([subView isKindOfClass:[CustomButton class]])
{
CustomButton *button = (CustomButton*)subView;
button.backgroundColor = [UIColor redColor];
[button setType:kButtonTypeQuestion];
button.titleLabel.font = kFontForContentPhone;
if (button.tag == 62254 || button.tag == 62263)
{
CGRect tempFrame = button.frame;
tempFrame.origin.x = button.frame.origin.x - 3.0f;
button.frame = tempFrame;
}
if (self.soundFile != nil)
{
CGRect tempFrame = button.frame;
tempFrame.size.width = button.frame.size.width + 28.0f;
button.frame = tempFrame;
[button setContentEdgeInsets:UIEdgeInsetsMake(5.0f, 30.0f, 5.0f, 10.0f)];
}
}
else if ([subView isKindOfClass:NSClassFromString(#"_UITextContainerView")]) {
// do a another loop
for (UIView *innerSubViews in subView.subviews) {
if([subView isKindOfClass:[CustomButton class]])
{
// call your custom button method here
break; // stop inner loop
}
}
}
}
}
The UIScrollView (upon calling from a button click action) will only scroll a few pixels short of the bottom of the content size window. This problem only occurs when called from a certain function.
- (void)adjustScrollView {
SVscrollableLayout.contentSize = CGSizeMake(SVscrollableLayout.frame.size.width, CGRectGetMaxY(bottomSubview.frame));
}
Problematic Function:
- (IBAction)BTNdeleteAction:(id)sender {
[self clearSubviews];
//Do some stuff to remove the deleted subview
[self displaySubviews];
[self adjustScrollView];
}
- (void)temporarilyClearClasses {
int numberOfClasses = [self loadNumberOfClasses];
for (int i=0; i<numberOfClasses; i++) {
UIButton* TXTclassDisplayTemp = (UIButton*)[self.view viewWithTag:100+i];
[TXTclassDisplayTemp removeFromSuperview];
UIButton* BTNdeleteButtonTemp = (UIButton*)[self.view viewWithTag:200+i];
[BTNdeleteButtonTemp removeFromSuperview];
}
}
- (void)printClasses {
int numberOfClasses = [self loadNumberOfClasses];
NSArray* classNames = [NSArray arrayWithArray:[self loadClassesArray]];
int yPlacement = 0;
if (numberOfClasses != 0) {
if (noClasses == YES)
for (int i=0; i<numberOfClasses; i++) {
UIButton* TXTnewClass = [[UIButton alloc] initWithFrame:CGRectMake(0, 10+(50*i), self.view.frame.size.width, 35)];
TXTnewClass.tag = 100+i;
[TXTnewClass addTarget:self
action:#selector(TXTclassDisplayAction:)
forControlEvents:UIControlEventTouchUpInside];
deleteAllButtonYplacement = CGRectGetMaxY(TXTnewClass.frame)+10 ;
UIButton* BTNdeleteClass = [UIButton buttonWithType:UIButtonTypeCustom];
BTNdeleteClass.frame = CGRectMake(10, yPlacement+10+(50*i), 70, 35);
BTNdeleteClass.tag = 200+i;
[BTNdeleteClass setTitle:#"Delete" forState:UIControlStateNormal];
if ([self editingStatus] == OFF)
BTNdeleteClass.hidden = YES;
[SVscrollableLayout addSubview:BTNdeleteClass];
if (CGRectGetMaxY(TXTnewClass.frame) <= SVscrollableLayout.frame.size.height) {
SVscrollableLayout.contentSize = SVscrollableLayout.frame.size;
}
else {
while (CGRectGetMaxY(TXTnewClass.frame) > SVscrollableLayout.contentSize.height) {
SVscrollableLayout.contentSize = CGSizeMake(SVscrollableLayout.frame.size.width, SVscrollableLayout.contentSize.height + 5);
}
}
}
}
else {
TXTnoClasses.hidden = NO;
BTNeditClassList.enabled = NO;
BTNeditClassList.alpha = 0.4;
}
float centerHorizontal = self.view.frame.size.width / 2;
float deleteAllButtonXplacement = centerHorizontal - (93/2) - 3;
NSLog(#"%f", BTNdeleteAll.frame.size.width);
[self refreshDeleteButton:CGRectMake(deleteAllButtonXplacement, deleteAllButtonYplacement, 93, 44)];
[self adjustScrollView];
}
In one of the projects I'm working on, I have a UIScrollView that has a lot of buttons and labels that resemble movie theater seats that the user can tap to select seats for them to buy. Is it possible to zoom the UIScrollView? I have already declared the UIScrollViewDelegate, set the delegate to self, set the minimum and maximum zoom scale but I'm having issues with the viewForZoomingInScrollView and scrollViewDidEndZooming methods. I tried to return my scrollview on the viewForZoomingInScrollView method but it crashes.
Here's some of the code:
- (void) renderSeatMapOnScreen
{
int x_offset_row = 10;
int y_offset_row = 25;
int x_offset_col = 30;
int y_offset_col = 0;
int scrollview_w = 0;
int scrollview_h = 0;
NSString *row_name = #"";
int tag_index = 0;
NSMutableArray *seat_map_table = [seat_map_xml seats_map];
NSString *seat_available_str = [seat_avail_xml seat_available];
HideNetworkActivityIndicator();
NSLog(#"Seats available = %#", seat_available_str);
NSArray *seats_avail = [seat_available_str componentsSeparatedByString:#";"];
int ROW_COL_OFFSET = 25;
for (int row = 0; row < [seat_map_table count]; row++)
{
UILabel * rowlabel = [[UILabel alloc] init];
rowlabel.frame = CGRectMake(x_offset_row , y_offset_row, 22, 22);
SeatMapDeclaration *rowmap = [seat_map_table objectAtIndex:row];
rowlabel.text = rowmap.rows;
[scrollview addSubview:rowlabel];
row_name = [NSString stringWithFormat:#"%#", rowmap.rows];
NSArray *seat = [rowmap.rowmap componentsSeparatedByString:#";"];
NSLog(#"row[%i] = %#", row, rowmap.rowmap);
if (row == 0)
{
scrollview_w = [seat count] * ROW_COL_OFFSET + y_offset_row;
total_column = [seat count];
total_row = [seat_map_table count];
}
x_offset_col = 30 ;
y_offset_col = y_offset_row ;
for (int column = 0; column < [seat count]; column++)
{
if (([[seat objectAtIndex:column] rangeOfString:#"a("].location != NSNotFound)
|| ([[seat objectAtIndex:column] isEqualToString:#""]))
{
CGRect myImageRect = CGRectMake(x_offset_col, y_offset_col, 22, 22);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:myImageRect];
[imageView setImage:[UIImage imageNamed:#"noseat.png"]];
[scrollview addSubview:imageView];
}
else if ([[seat objectAtIndex:column] rangeOfString:#"b("].location != NSNotFound)
{
CGRect myImageRect = CGRectMake(x_offset_col, y_offset_col, 22, 22);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:myImageRect];
imageView.image = [UIImage imageNamed:#"noseat.png"];
[scrollview addSubview:imageView];
}
else
{
NSString *status = [NSString stringWithFormat:#"%#%#", row_name, [seat objectAtIndex:column]];
BOOL matchedFound = NO;
for (int i = 0; i < [seats_avail count]; i++)
{
if ([[seats_avail objectAtIndex:i] isEqualToString:status])
{
matchedFound = YES ;
break ;
}
}
if (matchedFound == YES)
{
UIButton * seat_btn = [UIButton buttonWithType:UIButtonTypeCustom];
[seat_btn setBackgroundImage:[UIImage imageNamed:#"seatavailable.png"] forState:UIControlStateNormal];
[seat_btn setBackgroundImage:[UIImage imageNamed:#"seatactive.png"] forState:UIControlStateSelected];
seat_btn.frame = CGRectMake(x_offset_col, y_offset_col, 22, 22);
tag_index = [[seat objectAtIndex:column] intValue];
[seat_btn setTitle:[seat objectAtIndex:column] forState:UIControlStateNormal];
seat_btn.titleLabel.font = [UIFont fontWithName:#"Helvetica" size:12.0];
seat_btn.tag = tag_index + row * 100;
[seat_btn addTarget:self
action:#selector(checkboxButton:)
forControlEvents:UIControlEventTouchUpInside];
[scrollview addSubview:seat_btn];
}
else
{
CGRect myImageRect = CGRectMake(x_offset_col, y_offset_col, 22, 22);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:myImageRect];
imageView.image = [UIImage imageNamed:#"seat_not_available.png"];
[scrollview addSubview:imageView];
}
}
x_offset_col += ROW_COL_OFFSET ;
NSString *data = #"";
[seatControl addObject:data];
}
y_offset_row += ROW_COL_OFFSET;
}
UILabel *screenlabel = [[UILabel alloc] init];
screenlabel.frame = CGRectMake((scrollview_w-300)/2, 3, 300, 15);
screenlabel.text = #"Screen";
screenlabel.backgroundColor = [UIColor blackColor];
screenlabel.textColor = [UIColor whiteColor] ;
screenlabel.font = [UIFont fontWithName:#"Helvetica" size:10.0];
screenlabel.textAlignment = UITextAlignmentCenter;
[scrollview addSubview:screenlabel];
scrollview_h = y_offset_row + ROW_COL_OFFSET;
// Adjust scroll view
[scrollview setContentSize:CGSizeMake(scrollview_w, scrollview_h )];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
NSLog(#"1");
return scrollview;
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
NSLog(#"2");
}
In:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
NSLog(#"1");
return scrollview;
}
you are not supposed to return the UIScrollView itself, rather its subview that you would like to scroll/zoom.
If you have many subviews, I would suggest creating a container view for all of them that you return from viewForZoomingInScrollView; instead of adding your seats to the scrollview itself you would then add them to the container view.
If you want to zoom to a certain seat in the UIScrollView, you should use zoomToRect: animated:. You can specify a certain rectangle (i.e. the seat) to which you would like to zoom in on.
Zooming only works when you implement the viewForZoomingInScrollView: delegate callback.
UIImageView *tempImage = [[UIImageView alloc]initWithImage:[UIImage imageWithData:data]];
self.imageView = tempImage;
scrollView.contentSize = CGSizeMake(imageView.frame.size.width , imageView.frame.size.height);
scrollView.maximumZoomScale = 1;
scrollView.minimumZoomScale = .37;
scrollView.clipsToBounds = YES;
scrollView.delegate = self;
[scrollView addSubview:imageView];
scrollView.zoomScale = .37;
-(UIView *) viewForZoomingInScrollView:(UIScrollView *)inScroll {
return imageView;
}
If i move the scroll view horizontally it has to show new table with new labels inside it with one question at a time..The number of questions may be 10 or 20..means we have to create new table and new labels every time when scroll view moves horizontally..
First time when user selects the category the delegate method will call.In this method also i am creating table view and labels as shown below only..
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
{
#try {
cancelScroll = NO;
if (scrollEnabled) {
scrollEnabled = NO;
NSLog(#"Begin Scroll Dragging");
NSLog(#"%f -- %f",scrollView.contentOffset.x,scrollView.contentOffset.y);
if (scrollDirection == 1) {
scrollDirection = 0;
CGRect rect = mainTable.frame;
[scrollTableView scrollRectToVisible:rect animated:NO];
return;
}
else if (scrollDirection == 2) {
scrollDirection = 0;
HUDProgress.alpha=0;
CGFloat pageWidth = scrollTableView.frame.size.width;
int page;
if (pageControlBeingUsed) {
page = pageControl.currentPage;
// pageControlBeingUsed = NO;
}
else {
page= floor((scrollTableView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
}
NSLog(#"%d",page);
if (page == [questionTextArray count] - 1) {
if (maxValueReached) {
CGRect rect = mainTable.frame;
[scrollTableView scrollRectToVisible:rect animated:NO];
return;
}
}
photoAlbumView.alpha = 0;
photoView.alpha = 0;
FileAddView.alpha = 0;
settingsView.alpha = 0;
refreshView.alpha = 0;
photoAlbumViewPortrait.alpha = 0;
FileAddViewPortrait.alpha = 0;
settingsViewPortrait.alpha = 0;
refreshViewPortrait.alpha = 0;
checkValueForTableView = TRUE;
if (orientation == 1 ) {
if (!expandAction) {
scrollTableView.contentSize = CGSizeMake([questionTextArray count] * 720.0, 596.0);
}
else {
scrollTableView.contentSize = CGSizeMake([questionTextArray count] * 930.0, 596.0);
}
}
else {
if (!expandAction) {
scrollTableView.contentSize = CGSizeMake([questionTextArray count] * 520.0, 680.0);
}
else {
scrollTableView.contentSize = CGSizeMake([questionTextArray count] * 720.0, 680.0);
}
}
if (page >= 0) {
if (page > tempPage) {
int difference = page - tempPage;
NSLog(#"%d",difference);
NSLog(#"%f",tableXPosition);
tableXPosition = (tableXPosition + (difference * tableWidthPosition));
NSLog(#"%f",tableXPosition);
}
else if (page < tempPage){
int difference = tempPage - page;
NSLog(#"%d",difference);
tableXPosition = abs ((difference * tableWidthPosition) - tableXPosition);
NSLog(#"%f",tableXPosition);
}
else {
CGRect rect = mainTable.frame;
[scrollTableView scrollRectToVisible:rect animated:NO];
}
}
if([questionTextArray count] > 1)
{
pageControl.numberOfPages = [questionTextArray count];
[pageControl addTarget:self action:#selector(changePage) forControlEvents:UIControlEventValueChanged];
if(scrollTableView != nil)
{
if (mainTable != nil) {
[mainTable release];
mainTable = [[UITableView alloc] initWithFrame:CGRectMake(tableXPosition , 45.0, tableWidthPosition, 565.0) style:UITableViewStyleGrouped];
}
mainTable.delegate = self;
mainTable.dataSource = self;
mainTable.scrollEnabled = YES;
mainTable.backgroundColor = [UIColor clearColor];
[mainTable reloadData];
[scrollTableView addSubview:mainTable];
}
NSLog(#"%d %d",page,previousPage);
if (page >= 0) {
if (previousPage != page) {
pageControl.currentPage= page;
}
}
if (page >= 0) {
if (previousPage != page) {
UIImageView *imgView = [[UIImageView alloc] init];
UILabel *questionTextLbl1 = [[UILabel alloc] init];
UILabel *questionNumberLbl1 = [[UILabel alloc] init];
if (orientation == 1) {
if (!expandAction) {
imgView.frame = CGRectMake(tableXPosition, 0.0, 710.0, 39.0);
questionTextLbl1.frame = CGRectMake(10.0, 2.0, 430.0, 35.0);
questionNumberLbl1.frame = CGRectMake(550.0, 2.0, 200.0, 35.0);
}
else {
imgView.frame = CGRectMake(tableXPosition, 0.0, 920.0, 39.0);
questionTextLbl1.frame = CGRectMake(50.0, 2.0, 500.0, 35.0);
questionNumberLbl1.frame = CGRectMake(800.0, 2.0, 150.0, 35.0);
compressButton = [UIButton buttonWithType:UIButtonTypeCustom];
compressButton.frame = CGRectMake(10.0, 10.0, 25.0, 25.0);
[compressButton setImage:[UIImage imageNamed:#"Drop Down small2.png"] forState:UIControlStateNormal];
[compressButton addTarget:self action:#selector(compressAction) forControlEvents:UIControlEventTouchUpInside];
}
imgView.contentMode = UIViewContentModeScaleToFill;
imgView.backgroundColor = [UIColor clearColor];
imgView.image = [UIImage imageNamed:#"Top gary header.png"];
[scrollTableView addSubview:imgView];
if (!expandAction) {
}
else {
compressButton.backgroundColor = [UIColor clearColor];
[compressButton addTarget:self action:#selector(compressAction) forControlEvents:UIControlEventTouchUpInside];
[imgView addSubview:compressButton];
imgView.userInteractionEnabled = YES;
}
questionTextLbl1.backgroundColor = [UIColor clearColor];
questionTextLbl1.numberOfLines = 0;
questionTextLbl1.text = [NSString stringWithFormat:#"%d.%#?",page + 1,[questionTextArray objectAtIndex:page]];
NSLog(#"Question arry %#", questionTextArray);
[imgView addSubview:questionTextLbl1];
if([questionTextArray count] > 1)
{
questionNumberLbl1.text = [NSString stringWithFormat:#"Question %d/%d",page + 1,[questionTextArray count]];
}
else {
questionNumberLbl1.text = [NSString stringWithFormat:#"Question 1"];
}
questionNumberLbl1.backgroundColor = [UIColor clearColor];
questionNumberLbl1.numberOfLines = 0;
[imgView addSubview:questionNumberLbl1];
}
else {
if (!expandAction) {
imgView.frame = CGRectMake(tableXPosition, 0.0, 530.0, 38.0);
questionTextLbl1.frame = CGRectMake(10.0, 2.0, 350.0, 35.0);
questionNumberLbl1.frame = CGRectMake(380.0, 2.0, 150.0, 35.0);
}
else {
imgView.frame = CGRectMake(tableXPosition, 0.0, 720.0, 38.0);
questionTextLbl1.frame = CGRectMake(40.0, 2.0, 500.0, 35.0);
questionNumberLbl1.frame = CGRectMake(550.0, 2.0, 150.0, 35.0);
compressButton = [UIButton buttonWithType:UIButtonTypeCustom];
compressButton.frame = CGRectMake(10.0, 10.0, 25.0, 25.0);
[compressButton setImage:[UIImage imageNamed:#"Drop Down small2.png"] forState:UIControlStateNormal];
[compressButton addTarget:self action:#selector(compressAction) forControlEvents:UIControlEventTouchUpInside];
}
imgView.contentMode = UIViewContentModeScaleToFill;
imgView.backgroundColor = [UIColor clearColor];
imgView.image = [UIImage imageNamed:#"Top gary header.png"];
[scrollTableView addSubview:imgView];
if (!expandAction) {
}
else {
[imgView addSubview:compressButton];
imgView.userInteractionEnabled = YES;
}
questionTextLbl1.backgroundColor = [UIColor clearColor];
questionTextLbl1.numberOfLines = 0;
questionTextLbl1.font = [UIFont systemFontOfSize:14.0];
questionTextLbl1.text = [NSString stringWithFormat:#"%d.%#?",page + 1,[questionTextArray objectAtIndex:page]];
NSLog(#"Question arry %#", questionTextArray);
[imgView addSubview:questionTextLbl1];
if([questionTextArray count] > 1)
{
questionNumberLbl1.text = [NSString stringWithFormat:#"Question %d/%d",page + 1,[questionTextArray count]];
}
else {
questionNumberLbl1.text = [NSString stringWithFormat:#"Question 1"];
}
questionNumberLbl1.backgroundColor = [UIColor clearColor];
questionNumberLbl1.numberOfLines = 0;
[imgView addSubview:questionNumberLbl1];
}
[mainTable reloadData];
// [scrollTableView addSubview:mainTable];
[imgView release];
[questionNumberLbl1 release];
[questionTextLbl1 release];
//
CGRect rect = mainTable.frame;
if (pageControlBeingUsed) {
[scrollTableView scrollRectToVisible:rect animated:YES];
pageControlBeingUsed = NO;
}
else {
[scrollTableView scrollRectToVisible:rect animated:NO];
}
tempPage = page;
previousPage = page;
if (page == [questionTextArray count] - 1){
maxValueReached = YES;
}
else {
maxValueReached = NO;
}
}
}
NSLog(#"page:%d previous page:%d temp page:%d",page,previousPage,tempPage);
// sleep(0);
}
}
}
else {
[self killScroll];
scrollDirection = 0;
}
}
#catch (NSException *exception) {
NSLog(#"Exception: %#---%#",[exception name],[exception reason]);
}
#finally {
NSLog(#"Finally block");
}
}
It is working fine in simulator but in iPad for some time it is working fine while scrolling
after scrolling 15 or 20 times it is crashing..
Some times when running with instruments also crash log not showing anything and sometimes it is showing memory warning..
I have UIScrollView in stage and paging functionality enabled to move back/next.
I have added 10 subviews in UIScrollView. When I have modify the content inside the subviews, then it is not reflected in UIScrollView.
Book.m
- (void) initializeWithXML:(NSString *)XMLURLString {
NSData *xmlData;
NSString *url;
if ( ![applicationData getMode] ) {
resourceRootURL = [[applicationData getAssetsPath] stringByAppendingPathComponent:#"phone/"];
} else {
resourceRootURL = [applicationData getAssetsPath];
}
url = [resourceRootURL stringByAppendingPathComponent:XMLURLString];
//NSLog(#"Root URL...%#", url);
if ([url rangeOfString:#"http"].location != NSNotFound) {
xmlData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
} else {
xmlData = [NSData dataWithContentsOfFile:url];
}
bookContentArray = [self grabXML:xmlData andQuery:#"//page"];
// view controllers are created lazily
// in the meantime, load the array with placeholders which will be replaced on demand
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < kNumberOfPages; i++) {
[controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
[controllers release];
for(int i = 0; i < [bookContentArray count]; i++) {
[viewControllers addObject:[NSNull null]];
}
if ( isTwoPage ) {
kNumberOfPages = [bookContentArray count];
} else {
kNumberOfPages = [bookContentArray count]/2;
}
CGRect pagingScrollViewFrame = [self frameForPagingScrollView];
scrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame];
scrollView.pagingEnabled = YES;
scrollView.backgroundColor = [UIColor blackColor];
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.contentSize = [self contentSizeForPagingScrollView];
scrollView.contentSize = CGSizeMake(scrollView.contentSize.width,scrollView.frame.size.height);
scrollView.delegate = self;
self.view = scrollView;
kNumberOfPages = [bookContentArray count];
pageControl.numberOfPages = kNumberOfPages;
pageControl.currentPage = 0;
// pages are created on demand
// load the visible page
// load the page on either side to avoid flashes when the user starts scrolling
[self loadPage:0];
}
- (void) loadPage:(int)number {
// Calculate which pages are visible
int firstNeededPageIndex = MAX(number-1, 0);
int lastNeededPageIndex = MIN(number+1, [viewControllers count] - 1);
//NSLog(#"%d,%d",firstNeededPageIndex,lastNeededPageIndex);
// Recycle no-longer-visible pages
for(int i = 0; i < [viewControllers count]; i++) {
ImageScrollView *page = [viewControllers objectAtIndex:i];
if ((NSNull *)page != [NSNull null]) {
if (page.index < firstNeededPageIndex || page.index > lastNeededPageIndex) {
//NSLog(#"removed page %d", page.index);
[page removeImages];
[page removeFromSuperview];
page = nil;
[viewControllers replaceObjectAtIndex:i withObject:[NSNull null]];
}
}
}
// load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
if ( number == 0 ) {
[self loadScrollViewWithPage:number];
[self loadScrollViewWithPage:number+1];
} else if ( number == [bookContentArray count]-1) {
[self loadScrollViewWithPage:number-1];
[self loadScrollViewWithPage:number];
} else {
[self loadScrollViewWithPage:number-1];
[self loadScrollViewWithPage:number];
[self loadScrollViewWithPage:number+1];
}
}
- (void)loadScrollViewWithPage:(int)page {
ImageScrollView *pageController = [[ImageScrollView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
pageController.index = page;
pageController.myDelegate = self;
pageNumber = pageControl.currentPage;
pageController.isTwoPage = isTwoPage;
if ( pageNumber == page ) {
pageController.isCurrentPage = YES;
} else {
pageController.isCurrentPage = NO;
}
[controller setImageURL:leftURL andRightURL:rightURL andPriority:0];
[scrollView addSubview:controller];
}
ImageScrollView.m
- (void) setImageURL:(NSString *)leftURL andRightURL:(NSString *)rightURL andPriority:(int)priority {
[imageView removeFromSuperview];
[imageView release];
imageView = nil;
if ( !isTwoPage ) {
imageView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
} else {
imageView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
}
if ( isTwoPage ) {
leftImage = [[FBEPage alloc] initWithNibName:#"FBEPage" bundle:nil];
leftImage.view.frame = CGRectMake(0, 43, 512, 682);
rightImage = [[FBEPage alloc] initWithNibName:#"FBEPage" bundle:nil];
rightImage.view.frame = CGRectMake(512, 43, 512, 682);
} else {
leftImage = [[FBEPage alloc] initWithNibName:#"FBEPage" bundle:nil];
leftImage.view.frame = imageView.frame;
rightImage = [[FBEPage alloc] initWithNibName:#"FBEPage" bundle:nil];
rightImage.view.frame = imageView.frame;
}
leftImage.delegate = self;
rightImage.delegate = self;
if ( isFirstPage ) {
[leftImage.view setHidden:YES];
[rightImage.view setHidden:NO];
} else if ( isLastPage ) {
[leftImage.view setHidden:NO];
[rightImage.view setHidden:YES];
} else if ( !isTwoPage ) {
[leftImage.view setHidden:NO];
[rightImage.view setHidden:YES];
} else {
[leftImage.view setHidden:NO];
[rightImage.view setHidden:NO];
}
imageView.backgroundColor = [UIColor whiteColor];
[self addSubview:imageView];
[leftImage setImageURL:leftURL];
[rightImage setImageURL:rightURL];
[imageView addSubview:leftImage.view];
[imageView addSubview:rightImage.view];
}
FBPage.m
- (void) setImageURL:(NSString *)url {
imageView = [[UIImageView alloc] init];
imageView.frame = CGRectMake(0, 0, 768, 1024);
imageView.center = self.view.center;
imageView.userInteractionEnabled = TRUE;
[container addSubview:imageView];
[activityIndicator setHidden:YES];
if ([url rangeOfString:#"http"].location != NSNotFound) {
SDWebImageManager *manager = [SDWebImageManager sharedManager];
UIImage *cachedImage = [manager imageWithURL:[NSURL URLWithString:url]];
if (cachedImage) {
[imageView setImage:cachedImage];
isImageLoaded = YES;
[self addScrollView];
} else {
[activityIndicator setHidden:NO];
[activityIndicator startAnimating];
[manager downloadWithURL:[NSURL URLWithString:url] delegate:self options:0 success:^(UIImage *image){
[activityIndicator stopAnimating];
[activityIndicator setHidden:YES];
[imageView setImage:image];
isImageLoaded = YES;
[self addScrollView];
} failure:nil];
}
imageURL = url;
} else {
imageView.image = [UIImage imageWithContentsOfFile:url];
isImageLoaded = YES;
[self addScrollView];
}
}
- (void) setHotspotURL:(NSString *)URL {
[hotspot.view removeFromSuperview];
[hotspot release];
hotspot = nil;
hotspot = [[FBHotspot alloc] initWithNibName:#"FBHotspot" bundle:nil];
hotspot.view.frame = CGRectMake(0, 0, 768, 1024);
hotspot.delegate = self;
[hotspot setURL:URL];
[container addSubview:hotspot.view];
}
**When I am calling setHotspotURL and it’s not updating to the scrollview.**
Make sure you inserted your content at the correct position within the scroll view. Set the contentSize accordingly. Try to call layoutSubviews of the UIScrollView instance