I am a newbie to IOS programming and currently i have a image application with swipe images on uiview. I have questions.
i want to swipe image to right when click on right button and swipe image to left when click left button in iphone.
i am using this code:-
.h file
#import<UIKit/UIKit.h>
#interface flashViewcontroller : UIViewController<UIScrollViewDelegate>
#property (nonatomic, strong) IBOutlet UIScrollView *scrollView;
#property (nonatomic, strong) IBOutlet UIPageControl *pageControl;
#property (nonatomic, strong) NSArray *pageImages;
#property (nonatomic, strong) NSMutableArray *pageViews;
#property(nonatomic,strong) IBOutlet UIButton *next;
#property(nonatomic,strong) IBOutlet UIButton *previous;
-(void)loadVisiblePages;
-(void)loadPage:(NSInteger)page;
-(void)purgePage:(NSInteger)page;
-(IBAction)next:(id)sender;
-(IBAction)previos:(id)sender;
-(IBAction)skipes:(id)sender;
#end
// Left Button Click
-(IBAction)click_LeftBtn:(id)sender{
[imgView setFrame:CGRectMake(imgView.frame.orign.x, imgView.frame.orign.y, imgView.frame.size.width, imgView.frame.size.height)];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.5]; //// Set Animationt time Duration
[imgView setFrame:CGRectMake(0, imgView.frame.orign.y, imgView.frame.size.width, imgView.frame.size.height)];
[UIView commitAnimations];
}
// Right Button Click
-(IBAction)click_RightBtn:(id)sender
{
[imgView setFrame:CGRectMake(imgView.frame.orign.x, imgView.frame.orign.y, imgView.frame.size.width, imgView.frame.size.height)];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.5]; //// Set Animationt time Duration
[imgView setFrame:CGRectMake(320-imgView.frame.size.width, imgView.frame.orign.y, imgView.frame.size.width, imgView.frame.size.height)];
[UIView commitAnimations];
}
Related
I have a custom UIView XIB that gets loaded onto my ViewController and the frame is adjusted to be collapsed before added to subview. The UIButton tied to my XIB is showing when the frame is smaller than the button location. How can I hide and show my UIButton as the frame is expanding/collapsing? My XIB is not using AutoLayout.
ViewController.m
self.greenView = [[[NSBundle mainBundle] loadNibNamed:#"Green" owner:self options:nil] objectAtIndex:0];
[self.navigationController.view addSubview:self.greenView];
GreenView.h
#interface GreenView : UIView
#property (weak, nonatomic) IBOutlet UIView *expandView;
#property (nonatomic, getter = isExpanded) BOOL expand;
#property (weak, nonatomic) IBOutlet UIButton *testButton;
- (IBAction)testButtonTapped:(id)sender;
#end
GreenView.m
#interface GreenView()
#property (nonatomic) UITapGestureRecognizer *tapGesture;
#end
#implementation GreenView
- (void)awakeFromNib {
CGRect frame = self.frame;
frame.size.height = self.expandView.frame.size.height;
frame.origin.y = 200;
self.frame = frame;
self.expand = NO;
[self.expandView addGestureRecognizer:self.tapGesture];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (UITapGestureRecognizer *)tapGesture {
if (!_tapGesture) {
_tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapClicked:)];
}
return _tapGesture;
}
- (void)tapClicked:(UIGestureRecognizer *)recognizer {
CGRect frame = self.frame;
self.expand = !self.expand;
frame.size.height = self.expand ? gvExpandedHeight : gvCollapsedHeight;
[UIView animateWithDuration:0.5 animations:^{
self.frame = frame;
} completion:^(BOOL finished) {
NSLog(#"Frame after animation: %#", NSStringFromCGRect(self.frame));
}];
}
- (IBAction)testButtonTapped:(id)sender {
NSLog(#"Test button tapped");
}
#end
Collapsed:
Expanded:
GreenView.xib:
GrenenView.xib Struts and Springs:
If I understood you just want to clip it, if the button is outside to the view bounds. In -awakeFromNib add:
self.clipsToBounds = YES;
Using this property you are telling the view to cut everything that is not inside its bounds, views caw draw subviews even if they are place outside. Is a little expensive by means of performance, if you use it a lot or during heavy animations.
One way around could be hide it while the view is collapsed.
You change the frame to frame.origin.y = 200;
I think you have to set the UIButton constraint relative to your GreenView in you nib file
I have ViewController with UITextField(top view) and UITextView(bottom view). I want move UITextView to the top of the view, when user start editing it.
When user start editing from UITextView everything is fine, but when user first want to edit UITextField and then UITextView (without hidding keyboard) its not working. UITextField is hidden, but UITextView don't change his frame.
I tried to use UIKeyboardDidShowNotification but its called only when keyboard pops up.
Code to reproduce problem:
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UITextViewDelegate>
#property (weak, nonatomic) IBOutlet UITextField *titleTF;
#property (weak, nonatomic) IBOutlet UITextView *bodyTV;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
-(void)textViewDidBeginEditing:(UITextView *)textView {
self.titleTF.hidden=YES;
CGRect newFrame=CGRectMake(20, 20, textView.frame.size.width, 100);
textView.frame=newFrame;
}
#end
Run app and click on UITextView, app will look like:
http://imageshack.us/photo/my-images/694/32568213.png
For now everything is fine (UITextField is hidden and UITextView is moved and resized).
Start app again. First click on UITextField and then click on UITextView. App look like this:
http://imageshack.us/photo/my-images/843/66433184.png
UITextField is hidden, but UITextView did not change his frame.
Move the view up, when the TextView editing begins,
Move it down when editing ends.
Just put these two methods,
-(void) textViewDidBeginEditing:(UITextView *)textView{
CGRect frame = self.view.frame;
frame.origin.y = -(textView.frame.origin.y + textView.frame.size.height);
[UIView animateWithDuration:0.3f animations:^{
self.view.frame = frame;
}];
}
-(void) textViewDidEndEditing:(UITextView *)textView{
CGRect frame = self.view.frame;
frame.origin.y = 0;
[UIView animateWithDuration:0.3f animations:^{
self.view.frame = frame;
}];
}
I can't seem to get scrollViewDidEndDecelerating called.
I have a scrollView with 2 Views inside. Now I need it to set a value to a label in the first view when the scrollview is finished scrolling to the second view.
Header File:
#interface ViewController: UIViewController
{
UIScrollView *scrollView;
UIView *view1;
UIView *view2;
}
#property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
#property (strong, nonatomic) IBOutlet UIView *view1;
#property (strong, nonatomic) IBOutlet UIView *view2;
#property (weak, nonatomic) IBOutlet UILabel *lbl;
Implementation File:
#synthesize scrollView, view1, view2;
-(void)viewDidLoad
{
self.view1=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
self.view2=[[UIView alloc] initWithFrame:CGRectMake(320, 0, 320, 480)];
[self.scrollView addSubView:self.view1];
[self.scrollView addSubView:self.view2];
self.scrollView.bounces=NO;
self.scrollView.contentSize=CGSizeMake(640,460);
[self.scrollView setShowHorizontalScrollIndicator:NO];
[self.scrollView scrollRectToVisible:CGRectMake(0, 0, 320, 416) animated:NO];
}
-(void)scrollViewDidEndDecelerating:(UIView *)scrollView
{
lbl.text=#"0";
}
I don't see anything wrong, it should be working. Can someone help me out? Would appreciate it.
scrollViewDidEndDecelerating is not called if the user is scrolling slowly (i.e. the scroll view does not continue to scroll on touch up). In that case the delegate calls scrollViewDidEndDragging. So to do something when the user has stopped scrolling and the scrollview has stopped you can combine them:
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
if !decelerate {
endOfScroll()
}
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
endOfScroll()
}
func endOfScroll() {
//The user finished scrolling
}
In Objective-C
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
if(!decelerate) [self endOfScroll];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
[self endOfScroll];
}
-(void)endOfScroll{
//Do something
}
Either connect the delegate property of the scrollview to the File's Owner object in Interface Builder or just set the delegate manually in your ViewController's ViewDidLoad.
scrollview.delegate = self
This would do:
Header File:
#interface ViewController: UIViewController <UIScrollViewDelegate> //promise that you'll act as scrollView's delegate
{
UIScrollView *scrollView;
UIView *view1;
UIView *view2;
}
#property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
#property (strong, nonatomic) IBOutlet UIView *view1;
#property (strong, nonatomic) IBOutlet UIView *view2;
#property (weak, nonatomic) IBOutlet UILabel *lbl;
Implementation File:
#synthesize scrollView, view1, view2;
-(void)viewDidLoad
{
self.view1=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
self.view2=[[UIView alloc] initWithFrame:CGRectMake(320, 0, 320, 480)];
[self.scrollView addSubView:self.view1];
[self.scrollView addSubView:self.view2];
self.scrollView.bounces=NO;
self.scrollView.contentSize=CGSizeMake(640,460);
[self.scrollView setShowHorizontalScrollIndicator:NO];
[self.scrollView scrollRectToVisible:CGRectMake(0, 0, 320, 416) animated:NO];
[self.scrollView setDelegate:self];//Set delegate
}
-(void)scrollViewDidEndDecelerating:(UIView *)scrollView
{
lbl.text=#"0";
}
Swift 5
When scrolling UIScrollView programmatically, the scrollViewDidEndDecelerating is not called , but you can still use the scrollViewDidEndScrollingAnimation delegate function as a substitude for that, however there are some limitations stated in Apple documentation :
The scroll view calls this method at the end of its implementations of the setContentOffset(:animated:) and scrollRectToVisible(:animated:) methods, but only if animations are requested.
Therefore if you are not using default animated:true, but rather animating scrolling with custom UIView.animate animation (for example to change scrolling speed) , the only delegate method that still executes is scrollViewDidScroll
My animation currently animates my new view to come in from the bottom. I would rather have it do a horizontal flip instead.
How can I edit my code to result in this? Thanks.
-(void) aboutButtonPressed
{
[aboutView setFrame:CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:aboutView];
[UIView animateWithDuration:.5 animations:^{
[aboutView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
}];
}
Update:
#interface AboutViewController : UIViewController {
NSMutableArray *array1;
NSMutableArray *array2;
}
#property (nonatomic, retain) IBOutlet UITableView *aboutTableView;
#property (nonatomic, retain) IBOutlet UINavigationBar *navBar;
#property (nonatomic, retain) IBOutlet UIBarButtonItem *doneButton;
#property (nonatomic, retain) NSArray *array1;
#property (nonatomic, retain) NSArray *array2;
I think the flip animation you are desiring comes from presentModalViewController:animated: which can be found in the UIViewController Documentation
What you would want is to turn your aboutView into a AboutViewController:
-(void) aboutButtonPressed
{
AboutViewController *viewController = [[AboutViewController alloc] initWithNibName:#"AboutViewController" bundle:nil];
viewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
viewController.delegate = self;
[self presentModalViewController:viewController animated:YES];
[viewController release];
}
Use the method:
[UIView animateWithDuration:delay:options:animations:completion:]
for example:
[UIView animateWithDuration:0.5
delay:0.0
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
[aboutView setFrame:CGRectMake(0, 0, self.view.frame.size.width,
self.view.frame.size.height)];
}
completion:nil]
More on that in the UIView docs: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIView_Class/UIView/UIView.html
I have an animation on frame size which works fine when the UIButton is a UIButtonTypeRoundedRect. But has no visible affect when I am using a UIButtonStyleCustom with background image. My animation code is here:
[UIView beginAnimations:#"MyAnimation" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.25];
CGRect tempFrame = myButton.frame;
tempFrame.size.width = tempFrame.size.width + 100.0f;
myButton.frame = tempFrame;
[UIView commitAnimations];
Thanks for the help!
I've tried your example in my XCode. All works fine with both buttons. So some additional questions... Do you use Background or Image? What is view's mode (Scale to Fill or something else)? And where do you test your app (device or simulator, iphone 3 or iphone 4 or ...)?
Also make some checks. Check that myButton is connected in IB (maybe, you've created a new button and forgot to do this). Check that this code runs (maybe, you forgot connection to necessary touch action).
Hey guys, I finally resolve my problem. I subclassed UIView and added two UIImageViews and an UIButton to it. The animation is perfectly good now!
Here is the code:
.h
#import <UIKit/UIKit.h>
#interface NNAnimatableButton : UIView {
UIImageView *backgroundImageView;
UIImageView *imageView;
UIButton *button;
}
#property (nonatomic, retain) UIImageView *backgroundImageView;
#property (nonatomic, retain) UIImageView *imageView;
#property (nonatomic, retain) UIButton *button;
#end
.m
#import "NNAnimatableButton.h"
#implementation NNAnimatableButton
#synthesize backgroundImageView, imageView, button;
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
CGRect rect = CGRectMake(0.0f, 0.0f, frame.size.width, frame.size.height);
NSUInteger autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
// Initialization code.
backgroundImageView = [[UIImageView alloc] initWithFrame:rect];
backgroundImageView.autoresizingMask = autoresizingMask;
imageView = [[UIImageView alloc] initWithFrame:rect];
imageView.autoresizingMask = autoresizingMask;
imageView.contentMode = UIViewContentModeCenter;
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.autoresizingMask = autoresizingMask;
[button setFrame:rect];
[self addSubview:backgroundImageView];
[self addSubview:imageView];
[self addSubview:button];
[backgroundImageView release];
[imageView release];
}
return self;
}
- (void)dealloc {
[backgroundImageView release];
[imageView release];
[button release];
[super dealloc];
}
#end