Calculate ScrollView Offset:
typedef ns_enum (Nsinteger, scrolldirection) {
Scrolldirectionnone,
Scrolldirectionup,
Scrolldirectiondown,
};
Scrolldirection detectscrolldirection (currentoffsety, previousoffsety)
{
return currentoffsety > Previousoffsety? Scrolldirectionup:
Currentoffsety < previousoffsety? Scrolldirectiondown:
Scrolldirectionnone;
}
-(void) Scrollviewdidscroll: (Uiscrollview *) ScrollView
{
CGFloat currentoffsety = Scrollview.contentoffset.y;
Scrolldirection currentscrolldirection = detectscrolldirection (currentoffsety, _previousoffsety);
CGFloat topboundary =-scrollview.contentinset.top;
CGFloat bottomboundary = scrollView.contentSize.height + scrollView.contentInset.bottom;
BOOL isovertopboundary = currentoffsety <= topboundary;
BOOL isoverbottomboundary = currentoffsety >= bottomboundary;
BOOL isbouncing = (isovertopboundary && currentscrolldirection! = Scrolldirectiondown) | | (isoverbottomboundary && currentscrolldirection! = scrolldirectionup);
if (isbouncing | |!scrollview.isdragging) {
Return
}
CGFloat deltay = _previousoffsety-currentoffsety;
_accumulatedy + = DeltaY;
Switch (currentscrolldirection) {
Case Scrolldirectionup:
{
BOOL Isoverthreshold = _accumulatedy <-_upthresholdy;
if (Isoverthreshold | | isoverbottomboundary) {
Move Up DeltaY
}
}
Break
Case Scrolldirectiondown:
{
BOOL isoverthreshold = _accumulatedy > _downthresholdy;
if (Isoverthreshold | | isovertopboundary) {
Move Down DeltaY
}
}
Break
Case Scrolldirectionnone:
Break
}
Reset
if (!isovertopboundary &&!isoverbottomboundary && _previousscrolldirection! = currentscrolldirection ) {
_accumulatedy = 0;
}
_previousscrolldirection = currentscrolldirection;
_previousoffsety = currentoffsety;
}
-(void) scrollviewdidenddragging: (Uiscrollview *) ScrollView willdecelerate: (BOOL) decelerate
{
CGFloat currentoffsety = Scrollview.contentoffset.y;
CGFloat topboundary =-scrollview.contentinset.top;
CGFloat bottomboundary = scrollView.contentSize.height + scrollView.contentInset.bottom;
Switch (_previousscrolldirection) {
Case Scrolldirectionup:
{
BOOL Isoverthreshold = _accumulatedy <-_upthresholdy;
BOOL isoverbottomboundary = currentoffsety >= bottomboundary;
if (Isoverthreshold | | isoverbottomboundary) {
End Move Up
}
Break
}
Case Scrolldirectiondown:
{
BOOL isoverthreshold = _accumulatedy > _downthresholdy;
BOOL isovertopboundary = currentoffsety <= topboundary;
if (Isoverthreshold | | isovertopboundary) {
End DOWM
}
Break
}
Case Scrolldirectionnone:
Break
}
}
Iscomplete is completely set to show hidden, no words move navigtionbar according to the y-coordinate movement and set Navigationtitle color transparency values:
-(void) Movenavigtionbar: (cgfloat) DeltaY iscomplete: (bool) Iscomplete issethide: (BOOL) issethide
{
void (^setnavigationbarframeoriginy) (CGFloat originy) =^ (CGFloat originy) {
CGRect Currentnavigationbarframe = self.navigationcontroller.navigationbar.frame;//frame of the current navigation bar
Cgsize statubarframesize = [uiapplication sharedapplication].statusbarframe.size;//status bar size
CGFloat statusbarheight = uiinterfaceorientationisportrait (self.interfaceorientation)? statubarframesize.height:statubarframesize.width;//determine the height of the device rotation direction calculation status bar
CGFloat navigationbarheight = currentnavigationbarframe.size.height;//Current navigation bar height
CGFloat toplimit =-navigationbarheight + statusbarheight;//The height of the navigation bar how much is left in addition to the height of the status bar
CGFloat bottomlimit = statusbarheight;
CURRENTNAVIGATIONBARFRAME.ORIGIN.Y = Fmin (Fmax (Originy, Toplimit), bottomlimit);
CGFloat alpha = 1-(STATUSBARHEIGHT-CURRENTNAVIGATIONBARFRAME.ORIGIN.Y)/statusbarheight;//calculation transparency
Uicolor *titletextcolor = [Uicolor colorwithwhite:0.0 alpha:alpha]; Calculate the degree of concealment required for the title color
[UIView animatewithduration:0.1 animations:^{//Animation effect
Self.navigationController.navigationBar.frame = currentnavigationbarframe;// Assigns the final frame to Navigationbar (changes the frame value of the Navigationbar)
[Self.navigationController.navigationBar settitletextattributes:@{Nsforegroundcolorattributename:titletextcolor }];//assigns the calculated color value to the title of Navigationbar
Uicolor *tintcolor = Self.navigationController.navigationBar.tintColor;
if (Tintcolor) {
CGFloat *components = (CGFloat *) cgcolorgetcomponents (tintcolor.cgcolor);
Self.navigationController.navigationBar.tintColor = [Uicolor colorwithred:components[0] green:components[1] Blue: COMPONENTS[2] Alpha:alpha];
}
}];
};
if (!iscomplete) {//not fully set hide or show, set coordinates based on move up and down
CGRect Currentnavigationbarframe = self.navigationcontroller.navigationbar.frame;//frame of the current navigation bar
CGFloat nexty = currentnavigationbarframe.origin.y + deltay;//calculates the Framey of the navigation bar after the move
Setnavigationbarframeoriginy (nexty);
}else{
if (issethide) {
Cgsize statubarframesize = [UIApplication sharedapplication].statusbarframe.size;
CGFloat statusbarheight = uiinterfaceorientationisportrait (self.interfaceorientation)? StatuBarFrameSize.height:statuBarFrameSize.width;
CGFloat navigationbarheight = self.navigationController.navigationBar.frame.size.height;
CGFloat top =-navigationbarheight + statusbarheight;
Setnavigationbarframeoriginy (top);
}else{
Cgsize statubarframesize = [UIApplication sharedapplication].statusbarframe.size;
CGFloat statusbarheight = uiinterfaceorientationisportrait (self.interfaceorientation)? StatuBarFrameSize.height:statuBarFrameSize.width;
Setnavigationbarframeoriginy (Statusbarheight);
}
}
}
Iscomplete is completely set to show hidden, not the words to move the toolbar according to the y-coordinate movement:
-(void) Movetoolbar: (cgfloat) DeltaY iscomplete: (bool) Iscomplete issethide: (BOOL) issethide
{
void (^settoolbaroriginy) (CGFloat originy) =^ (CGFloat originy) {
CGRect currenttoolbarframe = self.navigationController.toolbar.frame;
CGFloat toolbarheight = currentToolBarFrame.size.height;
Cgsize viewsize = self.navigationController.view.frame.size;
CGFloat viewheight = uiinterfaceorientationisportrait (self.interfaceorientation)? ViewSize.height:viewSize.width;
CGFloat toplimit = viewheight-toolbarheight;
CGFloat bottomlimit = viewheight;
CURRENTTOOLBARFRAME.ORIGIN.Y = Fmin (Fmax (Originy, Toplimit), bottomlimit);
[UIView animatewithduration:0.1 animations:^{
Self.navigationController.toolbar.frame = Currenttoolbarframe;
}];
};
if (!iscomplete) {
CGRect currenttoolbarframe = self.navigationController.toolbar.frame;
CGFloat nexty = currenttoolbarframe.origin.y + DeltaY;
Settoolbaroriginy (nexty);
}else{
if (issethide) {
Cgsize viewsize = self.navigationController.view.frame.size;
CGFloat viewheight = uiinterfaceorientationisportrait (self.interfaceorientation)? ViewSize.height:viewSize.width;
Settoolbaroriginy (Viewheight);
}else{
Cgsize viewsize = self.navigationController.view.frame.size;
CGFloat viewheight = uiinterfaceorientationisportrait (self.interfaceorientation)? ViewSize.height:viewSize.width;
CGFloat toolbarheight = self.navigationController.toolbar.frame.size.height;
Settoolbaroriginy (Viewheight-toolbarheight);
}
}
}
-(void) Movetabbar: (cgfloat) DeltaY iscomplete: (bool) Iscomplete issethide: (BOOL) issethide
{
void (^settabbaroriginy) (CGFloat originy) =^ (CGFloat originy) {
CGRect frame = self.tabBarController.tabBar.frame;
CGFloat toolbarheight = frame.size.height;
Cgsize viewsize = self.tabBarController.view.frame.size;
CGFloat viewheight = uiinterfaceorientationisportrait (self.interfaceorientation)? ViewSize.height:viewSize.width;
CGFloat toplimit = viewheight-toolbarheight;
CGFloat bottomlimit = viewheight;
FRAME.ORIGIN.Y = Fmin (Fmax (Originy, Toplimit), bottomlimit);
[UIView animatewithduration:0.1 animations:^{
Self.tabBarController.tabBar.frame = frame;
}];
};
if (!iscomplete) {
CGRect frame = self.tabBarController.tabBar.frame;
CGFloat nexty = frame.origin.y + DeltaY;
Settabbaroriginy (nexty);
}else{
if (issethide) {
Cgsize viewsize = self.tabBarController.view.frame.size;
CGFloat viewheight = uiinterfaceorientationisportrait (self.interfaceorientation)? ViewSize.height:viewSize.width;
Settabbaroriginy (Viewheight);
}else{
CGFloat viewheight = self.tabBarController.view.frame.size.height;
CGFloat toolbarheight = self.tabBarController.tabBar.frame.size.height;
Settabbaroriginy (Viewheight-toolbarheight);
}
}
}
Slide Uiscrollview Hide Show navigation bar and tab bar