UIScrollView flickering with fixed subviews - objective-c

I use the following code to make "fixed" row headers in my UIScrollView descendant. It works well with the Simulator, but unfortunately, it flickers on the iPad. (To the left of the row header views, a white 1px line appears and disappears.) What can be improved?
- (void)initSubviews
{
const int ROW_COUNT = 20;
rowHeaderViews = [[NSMutableArray alloc]initWithCapacity:ROW_COUNT];
rowViews = [[NSMutableArray alloc]initWithCapacity:ROW_COUNT];
[self setContentSize:CGSizeMake(2000, [self frame].size.height)];
for (int i = 0; i < ROW_COUNT; i++)
{
UIView *header = [self createRowHeaderViewForRowNum:i];
[rowHeaderViews addObject:header];
UIView *row = [self createRowViewForRowNum:i];
[rowViews addObject:row];
[self addSubview:row];
[self addSubview:header];
}
[self layoutSubviews];
}
- (void)layoutSubviews
{
int x = [self contentOffset].x;
for (UIView *view in rowHeaderViews) {
[view setFrame:CGRectMake(x, view.frame.origin.y, view.frame.size.width, view.frame.size.height)];
}
}
- (UIView *)createRowHeaderViewForRowNum: (int)rowNum
{
UILabel *view = [[UILabel alloc]initWithFrame:CGRectMake(0, rowNum * 20, 200, 20)];
[view setBackgroundColor:[UIColor colorWithWhite:0.8*(20-rowNum)/20 alpha:1]];
[view setText:[NSString stringWithFormat:#"Row Header %d", rowNum]];
return view;
}
- (UIView *)createRowViewForRowNum: (int)rowNum
{
UILabel *view = [[UILabel alloc]initWithFrame:CGRectMake(200, rowNum * 20, 1800, 20)];
[view setBackgroundColor:[UIColor colorWithRed:rowNum/20.0 green:0 blue:0 alpha:1]];
[view setText:[NSString stringWithFormat:#"Row Content %d", rowNum]];
return view;
}
Thank you very much for any help!
EDIT: The iPad has got a Retina display. When using the Simulator with a "normal" iPad, it does not flicker. When switching the Simulator to a "Retina display" iPad, there is this flickering, too. Maybe this is something about the points/pixels difference?

I found the bug on my own. Sorry for bothering you.
In case anybody else has got the same problem:
contentOffset.x is a float value and contains .5 values for Retina displays.
Assigning it to an int variable in layoutSubviews caused the problem.
This is the fix (note I replaced 'int' with 'CGFloat'):
- (void)layoutSubviews
{
CGFloat x = [self contentOffset].x;
for (UIView *view in rowHeaderViews) {
[view setFrame:CGRectMake(x, view.frame.origin.y, view.frame.size.width, view.frame.size.height)];
}
}

Related

Content in Table cell of iOS 8 is not display

Everything works well in iOS 9.
But in iOS 8, the content is not displayed, only imageview is showed.
What i did is that I create a view and add label and UIImage to that view. Then that view is added to contentView of table Cell
create View
- (UIView *)getHeaderView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, HEADER_SECTION_HEIGHT)];
[view setBackgroundColor:[[RNThemeManager sharedManager] colorWithHexString:[_themeComponent valueForKey:#"Layer1Color"]]];
UILabel *taskName = [[UILabel alloc] initWithFrame:CGRectMake(5.0, SECTION_BREAK+3.0, view.frame.size.width-23.0, 20)];
[taskName setFont:[UIFont boldSystemFontOfSize:[[_themeComponent valueForKey:#"RegularFontSize"] intValue]]];
[taskName setText:combinedTask.taskDescription];
[taskName setTextColor: [[RNThemeManager sharedManager] colorWithHexString:[_themeComponent valueForKey:#"textPrimary"]]];
[view addSubview:taskName];
UIImageView *addressView = [[UIImageView alloc]initWithFrame:CGRectMake(5.0, SECTION_BREAK+28.0, 20, 20)];
addressView.image = [UIImage imageNamed:[NSString stringWithFormat:#"%#.png", #"Attr_Pin"]];
[view addSubview:addressView];
UILabel *address = [[UILabel alloc] initWithFrame:CGRectMake(35.0, SECTION_BREAK+23.0, view.frame.size.width-35.0, 30)];
[address setFont:[UIFont boldSystemFontOfSize:[[_themeComponent valueForKey:#"SmallFontSize"] intValue]]];
address.lineBreakMode = NSLineBreakByWordWrapping;
address.numberOfLines = 0;
[address setText:[NSString stringWithFormat:#"%#", unit]];
[address setTextColor: [[RNThemeManager sharedManager] colorWithHexString:[_themeComponent valueForKey:#"textPrimary"]]];
[view addSubview:address];
return view;
}
Add view to table cell
UIView *header= [self getHeaderView:tableView viewForHeaderInSection:section];
cell.taskIdLabel.hidden = YES;
[cell.contentView addSubview:header];
Anyone has any ideas? Thanks much for your help.
This is because iOS 9 Apple handle good autolayout than before.
Let add [cell layoutIfNeeded] before return cell.
Update
I have same problem before. Here is block of code that i use to solved my problem.
- (void) sizeHeaderToFit {
//call in viewLayoutSubView
UIView *myHeaderView = self.tableView.tableHeaderView;
[myHeaderView setNeedsLayout];
[myHeaderView layoutIfNeeded];
CGFloat height = [myHeaderView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
myHeaderView.frame = ({
CGRect headerFrame = myHeaderView.frame;
headerFrame.size.height = height;
headerFrame;
});
self.tableView.tableHeaderView = myHeaderView;
}
call it at viewDidLayoutSubView. Hope it helps bạn :))

Add UIScrollView in UIView programmatically

I have a question how to add UIScrollView in UIView, so that the UIScrollView could work properly.
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, container.frame.size.width/2, container.frame.size.height/2)];
scroll.pagingEnabled = YES;
scroll.scrollEnabled = YES;
[scroll setBackgroundColor:[UIColor redColor]];
NSInteger numberOfViews = 3;
for (int i = 0; i < numberOfViews; i++)
{
CGFloat xOrigin = i * container.frame.size.width/2;
UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(xOrigin, 0, container.frame.size.width, container.frame.size.height)];
awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
[scroll addSubview:awesomeView];
[awesomeView release];
}
scroll.contentSize = CGSizeMake(container.frame.size.width/2 * numberOfViews, container.frame.size.height);
[container addSubview:scroll];
Above code is from tutorial: http://idevzilla.com/2010/09/16/uiscrollview-a-really-simple-tutorial/
But it doesn't work for me.
EDIT:
If you have a problem that you have set up a scrollview properly but it's not working, make sure that you are not overlaying with another UIView your scrollview. That was my problem.
Solved!
You can use below code to add UIScrollView on yourView :-
Step 1:
Delegate "UIScrollViewDelegate" to your ViewController.h
for example:
#interface yourViewController:UIViewController<UIScrollViewDelegate>
Step 2:
//create you UIScrollView
UIScrollView *MyScrollVw= [[UIScrollView alloc]initWithFrame:CGRectMake(0 ,0 ,320 ,480)];
MyScrollVw.delegate= self;
[MyScrollVw setShowsHorizontalScrollIndicator:NO];
[MyScrollVw setShowsVerticalScrollIndicator:YES];
MyScrollVw.scrollEnabled= YES;
MyScrollVw.userInteractionEnabled= YES;
[yourView addSubview:MyScrollVw];
MyScrollVw.contentSize= CGSizeMake(320 ,1500);//(width,height)
Step 3:
you want to implement the scrollView Delegates in ViewController.m
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return imgView;
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSLog(#"Did end decelerating");
//do your code here
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
NSLog(#"Did scroll");
//do your code here
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
NSLog(#"Did end dragging");
//do your code here
}
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
NSLog(#"Did begin decelerating");
//do your code here
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
NSLog(#"Did begin dragging");
//do your code here
}
your
scroll.contentSize = CGSizeMake(container.frame.size.width * numberOfViews, container.frame.size.height);
[container addSubview:scroll];
Add this line before your for loop.

UIScrollView with UIImageview and gestures using transitionFromView acting strange

I made a view which holds a UIScrollview:
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 65, 300, 188)];
//BackViews will hold the Back Image
BackViews = [[NSMutableArray alloc] init];
for (int i=0; i<BigPictures.count; i++) {
[BackViews addObject:[NSNull null]];
}
FrontViews = [[NSMutableArray alloc] initWithCapacity:BigPictures.count];
[self.pageControl setNumberOfPages:BigPictures.count];
Then I add several UIImageviews containing images:
//BigPictures holds objects of type UIImage
for (int i = 0; i < BigPictures.count; i++) {
UIImageView *ImageView = [[UIImageView alloc] initWithImage:[BigPictures objectAtIndex:i]];
ImageView.frame = [self.scrollView bounds];
[ImageView setFrame:CGRectMake(self.scrollView.frame.size.width * i, ImageView.frame.origin.y, ImageView.frame.size.width, ImageView.frame.size.height)];
//this saves the FrontView for later (flip)
[FrontViews insertObject:ImageView atIndex:i];
[self.scrollView addSubview:test];
}
// Detect Single Taps on ScrollView
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(flip)];
[self.scrollView addGestureRecognizer:tap];
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * BigPictures.count, self.scrollView.frame.size.height);
self.scrollView.pagingEnabled = YES;
self.scrollView.delegate = self;
Ok so far so good. Now the method which does the flipImage part:
- (void)flip {
int currentPage = self.pageControl.currentPage;
UIView *Back = nil;
if ([BackViews objectAtIndex:currentPage] == [NSNull null]) {
//CreateBackView is just creating an UIView with text in it.
Back = [self CreateBackView];
[BackViews replaceObjectAtIndex:currentPage withObject:Back];
[UIView transitionFromView:[[self.scrollView subviews] objectAtIndex:currentPage] toView:[BackViews objectAtIndex:currentPage] duration:0.8 options:UIViewAnimationOptionTransitionFlipFromLeft completion:NULL];
} else {
[UIView transitionFromView:[[self.scrollView subviews] objectAtIndex:currentPage] toView:[FrontViews objectAtIndex:currentPage] duration:0.8 options:UIViewAnimationOptionTransitionFlipFromRight completion:NULL];
[BackViews replaceObjectAtIndex:currentPage withObject:[NSNull null]];
}
[self.view addSubview:Back];
[self rebuildScrollView];
}
This is what rebuildScrollView does:
- (void)rebuildScrollView
{
for (UIView *subview in self.scrollView.subviews) {
[subview removeFromSuperview];
}
for (int i = 0; i < BigPictures.count; i++) {
if ([BackViews objectAtIndex:i] == [NSNull null]) {
[self.scrollView addSubview:[FrontViews objectAtIndex:i]];
} else {
[self.scrollView addSubview:[BackViews objectAtIndex:i]];
}
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * BigPictures.count, self.scrollView.frame.size.height);
self.scrollView.pagingEnabled = YES;
self.scrollView.delegate = self;
}
So the behavior is the following:
If I click on the first image (1 of 3 in scrollview) the effect is the way I want it, meaning the frontimage turns around and shows the empty (white) back with some text in the middle
If I click on the second image, the image turns but the back is completely empty showing the grey background of the window. If I scroll to the other images, the still show the front image (as expected)
Now I click on the third image and its the same as 1) great.
Current layout is now [BackView, Nothing, Backview)
Lets run that again. But now I click on the last image and its the same as 2) :(
Any ideas whats going wrong here?
EDIT: Some new findings. I doubled the amount of pictures and this is how Front and Backviews are placed (after flipping each one). P = Picture & B = Backview.
P_1(B_1) - actually the only correct one
P_2(empty - should be B_2)
P_3(B_2 - should be B_3)
P_4(empty - should be B_4)
P_5(B_3 - should be B_5)
P_6(empty - should be B_6)
Did a complete rebuild and now it works. Really strange because I used the exact same code.

How to add sound while UIScrollview is scrolling in iphone sdk?

This is my code for adding sound while UIScrollview is scrolling in iPhone SDK. In this code, how to add the sound while using touch-events in scrolling?
Please give me your idea and suggestion or sample.
const CGFloat kScrollObjHeight = 151.0;
const CGFloat kScrollObjWidth =320.0;
const NSUInteger kNumImages = 5;
- (void)layoutScrollImages
{
UIImageView *view = nil;
NSArray *subviews = [scrollView1 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 += (kScrollObjWidth);
}
}
[scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scrollView1 bounds].size.height)];
}
- (void)viewDidLoad
{
self.view.backgroundColor = [UIColor clearColor];
[scrollView1 setBackgroundColor:[UIColor whiteColor]];
[scrollView1 setCanCancelContentTouches:NO];
scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView1.clipsToBounds = YES;
scrollView1.scrollEnabled = YES;
scrollView1.pagingEnabled = YES;
NSUInteger i;
for (i = 1; i <= kNumImages; i++)
{
NSString *imageName = [NSString stringWithFormat:#"snap%d.jpg", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
CGRect rect = imageView.frame;
rect.size.height = kScrollObjHeight;
rect.size.width = kScrollObjWidth;
imageView.frame = rect;
imageView.tag = i;
[scrollView1 addSubview:imageView];
[imageView release];
[self layoutScrollImages];
}
[super viewDidLoad];
}
add UIScrollViewDelegate to your interface and then play sound while the scrolling is in task in scrollViewWillBeginDragging state and stop scrolling while in scrollViewDidEndDragging state.
You can use AVAudioPlayer to play sound on iOS devices
UPDATE:
Tells the delegate when the scroll view is about to start scrolling the content.
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
Also, Tells the delegate when dragging ended in the scroll view.
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
Implement these in your code & put in your custom behavior
you mast use UIScrollViewDelegate method
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//play sound here
}

UIImagePickerController how to hide the flip camera button?

is there a way to hide the flip camera button inside the UIImagePickerController?
thanks for reading
!^_^!
I ended up using a custom subclass of UIImagePickerController to fix this (and other) issues:
#import "SMImagePickerController.h"
#implementation SMImagePickerController
void hideFlipButtonInSubviews(UIView *view) {
if ([[[view class] description] isEqualToString:#"CAMFlipButton"]) {
[view setHidden:YES];
} else {
for (UIView *subview in [view subviews]) {
hideFlipButtonInSubviews(subview);
}
}
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
hideFlipButtonInSubviews(self.view);
}
#end
You should be able to create an empty button inside an overlayview that you float on top of the flip camera button. I hacked the code below to test and it seemed to work. Give it a try.
UIView *cameraOverlayView = [[UIView alloc] initWithFrame:CGRectMake(screenSize.width - 100.0f, 5.0f, 100.0f, 35.0f)];
[cameraOverlayView setBackgroundColor:[UIColor blackColor]];
UIButton *emptyBlackButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 35.0f)];
[emptyBlackButton setBackgroundColor:[UIColor blackColor]];
[emptyBlackButton setEnabled:YES];
[cameraOverlayView addSubview:emptyBlackButton];
cameraUI.allowsEditing = YES;
cameraUI.showsCameraControls = YES;
cameraUI.delegate = self;
cameraUI.cameraOverlayView = cameraOverlayView;