UIScrollView in different versions - iOS - ios

I'm working on a UIScrollView in iPad. The Scroll view scrolls smoothly in iOS 5.0 ,but the animation of scrolling is not smoother in Versions below 5.0.
Is there any specific customization be done to handle this?Should I add some more functionality to run UIScrollView in iOS 4.x?
-- I am facing this problem when adding custom UIView into the UIScrollView.
Working Code :
{
timeLineScrollView.frame = CGRectMake(0, 0, 500, 500);
float posX = 0;
for(int i = 0;i < 10;i++)
{
UIView *samView = [[UIView alloc]initWithFrame:CGRectMake(posX, 10, 500, 300)];
posX = posX + 500;
[timeLineScrollView addSubview:samView];
}
timeLineScrollView.contentSize = CGSizeMake(500*10, timeLineScrollView.frame.size.height);
}
Not Working Code :
{
timeLineScrollView.frame = CGRectMake(0, 0, 500, 500);
float posX = 0;
for(int i = 0;i < 10;i++)
{
CustomView *samView = [[CustomView alloc]initWithFrame:CGRectMake(posX, 10, 500, 300)];
posX = posX + 500;
[timeLineScrollView addSubview:samView];
}
timeLineScrollView.contentSize = CGSizeMake(500*10, timeLineScrollView.frame.size.height);
}
The Custom View :
- (id)initWithFrame:(CGRect)frame withEvent:(VisitHistoryEvent*)eventDetails
{
self = [super initWithFrame:frame];
if (self) {
complaint = [[UILabel alloc]init];
}
return self;
}
-(void)layoutSubviews
{
complaint.frame = CGRectMake(0, 0, 100, 20);
[self addSubview:complaint];
}

Just a hunch... If you tried to create one UIView as a subview for the UIScrollView (with a width of 3000 to match your content) and added your UILabels to that view rather than directly to the UIScrollView, perhaps then iOS will be able to cache it and give you better perf.

Related

iOS 11.2 custom navigation bar infinite loop

Since the release of IOS 11.2 my app is encountering an infinite loop when pushing a view controller whose navigation controller has a custom navigation bar height. Did someone find the solution for this problem? Thanks.
-(void)layoutSubviews{
[super layoutSubviews];
float height = 42.5;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
height = 48;
}
imageView.frame = CGRectMake(0, 0, [[UIScreen mainScreen]bounds].size.width, height);
if(UI_USER_INTERFACE_IDIOM() ==UIUserInterfaceIdiomPad){
if(#available(iOS 11.0,*)){
self.frame =CGRectMake(0, 20,[[UIScreen mainScreen]bounds].size.width, 55); // this line provoke the infinite loop
for(UIView *aView in self.subviews){
if([NSStringFromClass([aView class]) isEqualToString: #"_UINavigationBarContentView"]){
aView.frame = CGRectMake(0, 10, aView.frame.size.width, aView.frame.size.height);
}
if([NSStringFromClass([aView class]) isEqualToString: #"_UIBarBackground"]){
aView.frame = CGRectMake(0, 0, aView.frame.size.width, self.frame.size.height );
}
}
}
}
}
I also had the same problem.
I solved by removing this
frame.size.height = customHeight
from layoutSubviews and then adding this
override var frame: CGRect {
get {
return CGRect(x: 0, y: 0, width: super.frame.width, height: customHeight)
}
set (value) {
super.frame = value
}
}
to my UINavigationBar subclass.
I left all other code in layoutSubviews as it was before. (nb)

How to create view based animation in objective c?

I have to add the following animation in my iOS app, I have used a scroll bar along with UITableView and achieved the top and bottom animation, but I'm still stuck at the middle animation part where the 4 UIViews come in a single horizontal line. Any suggestions?
http://www.image-maps.com/m/private/0/af8u4ulika9siddnf6k6hhrtg2_untitled-2.gif
Code:-
#implementation AnimatedView {
UIScrollView *mainScroll;
UIScrollView *backgroundScrollView;
UILabel *_textLabel;
UITableView *_commentsTableView;
UIView *menuView;
UIView *_commentsViewContainer;
UIView *fadeView;
UIImageView *imageView;
NSMutableArray *comments;
}
- (id)init {
self = [super init];
if (self) {
_mainScrollView = [[UIScrollView alloc] initWithFrame:[UIApplication sharedApplication].keyWindow.frame];
self.view = _mainScrollView;
_backgroundScrollView = [[UIScrollView alloc] initWithFrame:HEADER_INIT_FRAME];
imageView = [[UIImageView alloc] initWithFrame:HEADER_INIT_FRAME];
fadeView = [[UIView alloc] initWithFrame:imageView.frame];
_textLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 100.0f, 150.0f, 25.0f)];
menuView = [[UIView alloc] initWithFrame:CGRectMake(0,_textLabel.frame.size.height+150, self.view.frame.size.width+30, 180)];
[_backgroundScrollView addSubview:imageView];
[_backgroundScrollView addSubview:fadeView];
[_backgroundScrollView addSubview:menuView];
[_backgroundScrollView addSubview:_textLabel];
_commentsViewContainer = [[UIView alloc] init];
_commentsTableView = [[UITableView alloc] init];
_commentsTableView.scrollEnabled = NO;
_commentsTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
[self.view addSubview:_backgroundScrollView];
[_commentsViewContainer addSubview:_commentsTableView];
[self.view addSubview:_commentsViewContainer];
// fake data!
comments = [#[#"Array for tableview"] mutableCopy];
}
return self;
}
#pragma mark Scroll
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat delta = 0.0f;
CGRect rect = HEADER_INIT_FRAME;
// Here is where I do the "Zooming" image and the quick fade out the text and toolbar
if (scrollView.contentOffset.y < 0.0f) {
delta = fabs(MIN(0.0f, _mainScrollView.contentOffset.y));
_backgroundScrollView.frame = CGRectMake(CGRectGetMinX(rect) - delta / 2.0f, CGRectGetMinY(rect) - delta, CGRectGetWidth(rect) + delta, CGRectGetHeight(rect) + delta);
[_commentsTableView setContentOffset:(CGPoint){0,0} animated:NO];
} else {
delta = _mainScrollView.contentOffset.y;
_textLabel.alpha = 1.0f;
CGFloat backgroundScrollViewLimit = _backgroundScrollView.frame.size.height - kBarHeight;
// Here I check whether or not the user has scrolled passed the limit where I want to stick the header, if they have then I move the frame with the scroll view
// to give it the sticky header look
if (delta > backgroundScrollViewLimit) {
_backgroundScrollView.frame = (CGRect) {.origin = {0, delta - _backgroundScrollView.frame.size.height + kBarHeight}, .size = {self.view.frame.size.width, HEADER_HEIGHT}};
_commentsViewContainer.frame = (CGRect){.origin = {0, CGRectGetMinY(_backgroundScrollView.frame) + CGRectGetHeight(_backgroundScrollView.frame)}, .size = _commentsViewContainer.frame.size };
_commentsTableView.contentOffset = CGPointMake (0, delta - backgroundScrollViewLimit);
CGFloat contentOffsetY = -backgroundScrollViewLimit * kBackgroundParallexFactor;
[_backgroundScrollView setContentOffset:(CGPoint){0,contentOffsetY} animated:NO];
}
else {
_backgroundScrollView.frame = rect;
_commentsViewContainer.frame = (CGRect){.origin = {0, CGRectGetMinY(rect) + CGRectGetHeight(rect)}, .size = _commentsViewContainer.frame.size };
[_commentsTableView setContentOffset:(CGPoint){0,0} animated:NO];
[_backgroundScrollView setContentOffset:CGPointMake(0, -delta * kBackgroundParallexFactor)animated:NO];
}
}
}
- (void)viewDidAppear:(BOOL)animated {
_mainScrollView.contentSize = CGSizeMake(CGRectGetWidth(self.view.frame), _commentsTableView.contentSize.height + CGRectGetHeight(_backgroundScrollView.frame));
}
You don't need any scrollview to implement this really. All you need is 1 UITableView with 2 sections. First section has a single empty element (but set the row height to 0), and enable the header for both sections. You can use UIViews for the headerViews. Then, you only need to change the header height (with icon positioning) based on tableview Delegate scrollViewDidScroll. scrollViewDidScroll is also a delegate of UITableView since one of TableView's element inherits from UIScrollView.

Display some images on album cover in iOS

i want to display some images on album cover on my app like bellow image.
EDIT:-
i want to display this view in UICollectionViewCell and collectionView scrollDirection Horizontal with paging enabled.
any help will be appreciated. and thanks in advance.
I do not know if you understand the question correctly.
If you want to do what I see on the picture, so try this:
UIView *cover = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 500, 500)];
cover.backgroundColor = [UIColor blueColor];
int width = 200;
int height = 350;
int posx = 100;
int posy = 50;
for (int i = 0; i < 10; i++) {
//craete images
UIView *imagebg = [[UIView alloc] initWithFrame:CGRectMake(posx + arc4random_uniform(20), posy + arc4random_uniform(20), width, height)];
imagebg.backgroundColor = [UIColor whiteColor];
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, width - 40, height - 50)];
image.image = [UIImage imageNamed:#"loading-screen.png"];
[imagebg addSubview:image];
//rotate
double rads = arc4random_uniform(100)/10;
CGAffineTransform transform = CGAffineTransformRotate(imagebg.transform, rads);
imagebg.transform = transform;
//add as subview
[cover addSubview:imagebg];
}
[self.view addSubview:cover];

ScrollView inside scrollView in iOS

I am creating photo browser.
I create VC that will be display photo and it can zoom/zoom out it.
Also I create another VC that I use like a page control.
Now I can only page thought all photos and it works very good.
Also If I load image in my VC for single photo I can zoom/zoom out it.
Now my purpose is to combine it.
I want to have image pager VC that will be able go thought images and zoom/zoom out desire image. And then there is no space to zoom/zoom out it will be switch to other images.
I have tap gesture and pinch gesture in single image VC.
How can I separate this events? Or how can I achieve it?
My code for is
ImagePagerViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
[self createPages];
}
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
[self updatePages];
}
- (void)createPages
{
for (NSUInteger i = 0; i < self.dataSource.count; i++)
{
NSURL *url = self.dataSource[i];
AttachmentViewController *attachmentViewController = [[AttachmentViewController alloc] initWithUserType:ImageViewer setActiveMode:None setInitialSeconds:NULL];
attachmentViewController.imageURL = url;
[self.scrollView insertSubview:attachmentViewController.view atIndex:self.views.count];
[self.views addObject:attachmentViewController.view];
}
}
- (void)updatePages
{
CGFloat w = ScreenWidth;
CGFloat h = ScreenHeight;
for (NSUInteger i = 0; i < self.views.count; i++)
{
UIView *view = self.views[i];
view.frame = CGRectMake(i * w, 0, w, h);
}
self.scrollView.contentSize = CGSizeMake(self.dataSource.count * w, h);
}

XCODE Obj-C add UiimageView programatically in a for

So i need to add programatically 3 images using a for loop this is my code , it's not correct, i just tryed.
for(int i = 1; i < 4; i++){
UIImageView *[i] = [[UIImageView alloc] initWithFrame:CGRectMake([i]*50, 50, 250, 250)];
[self.view addSubview:[i]];
}
where [i]; will be 1 , 2 and 3 any help would be appreciated. Thanks
You syntax is wrong. This is more what you need.
for (int i=0;i<3;i++){
UIImageView *image=[[UIImageView alloc] initWithFrame:CGRectMake(i*50, 50, 250, 250)];
[self.view addSubView:image];
}
However, this is not much use to you as your images are not referenced from anywhere so you can not populate or adjust them easily.
Change the variable name from [i] to some name like imageToAdd.
CGFloat xMarging = 0;
CGFloat imageOriginX = 0;
CGFloat imageOriginY = 0;
CGFloat imageWidth = 250;
CGFloat imageHeight = 250;
NSInteger numerOfImages = 3;
for (NSInteger i = 0; i < numerOfImages; i++){
[self.view addSubview:[[UIImageView alloc] initWithFrame:CGRectMake(xMarging + i * imageWidth,
imageOriginY,
imageWidth,
imgageHeight)]];
}
assuming you want them side by side as in [][][]

Resources