UIView animated backgroundColor with drawRect: - ios

I have a view that is able to draw a rect on itself.
It is actually a subclass of UICollectionView but I'm just struggling with UIView specific stuff; the backgroundColor.
I simply added a UIPanGestureRecognizer, saved the start point at UIGestureRecognizerStateBegan and the end point at UIGestureRecognizerStateChanged. I then use the -drawRect: method to draw the actual path:
- (void)awakeFromNib {
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panGesture:)];
[self addGestureRecognizer:pan];
}
- (void)panGesture:(UIPanGestureRecognizer *)panRecon {
if([panRecon state] == UIGestureRecognizerBegan) {
startPoint = [panRecon locationInView:self];
}
else if([panRecon state] == UIGestureRecognizerChanged) {
endPoint = [panRecon locationInView:self];
[self setNeedsDisplay];
}
else if([panRecon state] == UIGestureRecognizerEnded /* || failed || cancelled */) {
startPoint = CGPointZero;
endPoint = CGPointZero;
[self setNeedsDisplay];
}
}
- (void)drawRect:(CGRect)rect {
if(!CGPointEqualToPoint(startPoint, CGPointZero) && !CGPointEqualToPoint(endPoint, CGPointZero)) {
CGRect selectionRect = CGRectMake(startPoint.x, startPoint.y, endPoint.x - startPoint.x, endPoint.y - startPoint.y);
[[UIColor colorWithWhite:1.0 alpha:0.3] setFill];
[[UIColor colorWithWhite:1.0 alpha:1.0] setStroke];
CGContextFillRect(UIGraphicsGetCurrentContext(), selectionRect);
CGContextStrokeRect(UIGraphicsGetCurrentContext(), selectionRect);
}
}
I now want to start the selection mode (which the rect should actually be) with the view flashing. I created a UIView animation for that:
- (void)startSelection {
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
[self setBackgroundColor:[UIColor whiteColor]];
} completion:^(BOOL finished){
if(finished) {
[UIView animateWithDuration:0.9 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
[self setBackgroundColor:[UIColor blackColor]];
} completion:nil];
}
}
}
The problem is: once I implement the -drawRect: method, UIView does not animate the backgroundColor change anymore. I already tried UIViewAnimationOptionAllowAnimatedContent and almost everything I could find on google, but I wasn't able to solve my problem.
Does anybody know how I can animate backgroundColor of an UIView and have -drawRect: implemented?

Related

change size of a rectangle during runtime in objective C

Hi guys i have a quick question
I am using xcode to build an app and I want to animate the size of a custom rectangle during runtime.
I have a UIView class where i create custom rectangles and draw them on the screen of the ViewController. The problem is, whenever i start the app, it just create all the rectangles and then shows the screen.
What i want it to do is to show the screen and then animate the rectangles (like a "growing-animation")
This is how i create the rectangles in the UIView class
-(void)newRectWithX:(int) x height:(int) height{
_rdmRed = arc4random_uniform(100);
_rdmRed /= 100;
_rdmGreen = arc4random_uniform(100);
_rdmGreen /= 100;
_rdmBlue= arc4random_uniform(100);
_rdmBlue /= 100;
_color = [UIColor colorWithRed:_rdmRed green:_rdmGreen blue:_rdmBlue alpha:0.6];
[UIView animateWithDuration:50 delay:10 options:UIViewAnimationOptionShowHideTransitionViews
animations:^{
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect rectangle = CGRectMake(x,0,4,height);
CGContextAddRect(context, rectangle);
CGContextSetFillColorWithColor(context, [_color CGColor]);
CGContextFillRect(context, rectangle);
}
completion:nil];
}
I didn't do anything in the ViewController class yet.
+ animateWithDuration:delay:options:animations:completion:
Animate changes to one or more views using the specified duration, delay, options, and completion handler.
To be able to use that method you will need to animate a view. Here is a chuck of code that will allow you to use that method and CoreGraphics.
#implementation View
{
UIView * view;
}
- (void)runAnimation {
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(setNeedsDisplay) userInfo:nil repeats:YES];
view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
[UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionShowHideTransitionViews animations:^{
view.frame = CGRectMake(0, 0, 20, 20);
} completion:nil];
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect rectangle = ((UIView *)[view.layer presentationLayer]).frame;
CGContextAddRect(context, rectangle);
CGContextSetFillColorWithColor(context, [UIColor cyanColor].CGColor);
CGContextFillRect(context, rectangle);
}
#end
I have this to move a rectangle, maybe can help you
if(destination.origin.x > 0){
destination.origin.x = 0;
[self.navigationController.view.superview bringSubviewToFront:self.navigationController.view];
}else{
destination.origin.x += _sideMenu.view.frame.size.width;
}
[UIView animateWithDuration:0.2 animations:^{
self.view.frame = destination;
}completion:^(BOOL finished) {
if(finished){
if (destination.origin.x > 0) {
[self.navigationController.view.superview bringSubviewToFront:_sideMenu.view];
} else {
}
}
}];
change the destination.origin to destination.size, like this
CGRect destination = self.view.frame;
if(destination.size.height > 10){
destination.size.height = 10;
[self.navigationController.view.superview bringSubviewToFront:self.navigationController.view];
}else{
destination.size.height += 10;
}
[UIView animateWithDuration:0.2 animations:^{
self.view.frame = destination;
}completion:^(BOOL finished) {
if(finished){
if (destination.size.height > 10) {
[self.navigationController.view.superview bringSubviewToFront:_sideMenu.view];
} else {
}
}
}];

MKAnnotationView drag state ending animation

I'm using the custom MKAnnotationView animations provided by Daniel here: Subclassing MKAnnotationView and overriding setDragState but I run into an issue.
After the pin drop animation, when I go to move the map the mkannotationview jumps back to its previous location before the final pin drop animation block is called.
It seems to me that dragState=MKAnnotationViewDragStateEnding is being called before the animation runs? How can I get around this issue and set the final point of the mkannotationview to be the point it's at when the animation ends?
#import "MapPin.h"
NSString *const DPAnnotationViewDidFinishDrag = #"DPAnnotationViewDidFinishDrag";
NSString *const DPAnnotationViewKey = #"DPAnnotationView";
// Estimate a finger size
// This is the amount of pixels I consider
// that the finger will block when the user
// is dragging the pin.
// We will use this to lift the pin even higher during dragging
#define kFingerSize 20.0
#interface MapPin()
#property (nonatomic) CGPoint fingerPoint;
#end
#implementation MapPin
#synthesize dragState, fingerPoint, mapView;
- (void)setDragState:(MKAnnotationViewDragState)newDragState animated:(BOOL)animated
{
if(mapView){
id<MKMapViewDelegate> mapDelegate = (id<MKMapViewDelegate>)mapView.delegate;
[mapDelegate mapView:mapView annotationView:self didChangeDragState:newDragState fromOldState:dragState];
}
// Calculate how much to life the pin, so that it's over the finger, no under.
CGFloat liftValue = -(fingerPoint.y - self.frame.size.height - kFingerSize);
if (newDragState == MKAnnotationViewDragStateStarting)
{
CGPoint endPoint = CGPointMake(self.center.x,self.center.y-liftValue);
[MapPin animateWithDuration:0.2
animations:^{
self.center = endPoint;
}
completion:^(BOOL finished){
dragState = MKAnnotationViewDragStateDragging;
}];
}
else if (newDragState == MKAnnotationViewDragStateEnding)
{
// lift the pin again, and drop it to current placement with faster animation.
__block CGPoint endPoint = CGPointMake(self.center.x,self.center.y-liftValue);
[MapPin animateWithDuration:0.2
animations:^{
self.center = endPoint;
}
completion:^(BOOL finished){
endPoint = CGPointMake(self.center.x,self.center.y+liftValue);
[MapPin animateWithDuration:0.1
animations:^{
self.center = endPoint;
}
completion:^(BOOL finished){
dragState = MKAnnotationViewDragStateNone;
if(!mapView)
[[NSNotificationCenter defaultCenter] postNotificationName:DPAnnotationViewDidFinishDrag object:nil userInfo:[NSDictionary dictionaryWithObject:self.annotation forKey:DPAnnotationViewKey]];
}];
}];
}
else if (newDragState == MKAnnotationViewDragStateCanceling)
{
// drop the pin and set the state to none
CGPoint endPoint = CGPointMake(self.center.x,self.center.y+liftValue);
[UIView animateWithDuration:0.2
animations:^{
self.center = endPoint;
}
completion:^(BOOL finished){
dragState = MKAnnotationViewDragStateNone;
}];
}
}
- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event
{
// When the user touches the view, we need his point so we can calculate by how
// much we should life the annotation, this is so that we don't hide any part of
// the pin when the finger is down.
fingerPoint = point;
return [super hitTest:point withEvent:event];
}
#end
I had the same problem, especially under iOS 8. After many hours of testing I believe that iOS keeps track of where it thinks self.center of the annotation is during the time that the state is MKAnnotationViewDragStateDragging. You need to use extreme caution if you animate self.center when handling MKAnnotationViewDragStateEnding. Read that as "I couldn't get that to work, ever."
Instead, I kept Daniel's original code when handling states MKAnnotationViewDragStateStarting and MKAnnotationViewDragStateCanceling, I animated self.center. When handling MKAnnotationViewDragStateEnding I animated self.transform instead of self.center. This maintains the actual location of the annotation and just changes how it is rendered.
This works well for me running either iOS 7.1 and iOS 8.0. Also fixed a bug in hitTest, and added some code to reselect the annotation after dragging or canceling. I think that is the default behavior of MKPinAnnotationView.
- (void)setDragState:(MKAnnotationViewDragState)newDragState animated:(BOOL)animated
{
if(mapView){
id<MKMapViewDelegate> mapDelegate = (id<MKMapViewDelegate>)mapView.delegate;
[mapDelegate mapView:mapView annotationView:self didChangeDragState:newDragState fromOldState:dragState];
}
// Calculate how much to lift the pin, so that it's over the finger, not under.
CGFloat liftValue = -(fingerPoint.y - self.frame.size.height - kFingerSize);
if (newDragState == MKAnnotationViewDragStateStarting)
{
CGPoint endPoint = CGPointMake(self.center.x,self.center.y-liftValue);
[UIView animateWithDuration:0.2
animations:^{
self.center = endPoint;
}
completion:^(BOOL finished){
dragState = MKAnnotationViewDragStateDragging;
}];
}
else if (newDragState == MKAnnotationViewDragStateEnding)
{
CGAffineTransform theTransform = CGAffineTransformMakeTranslation(0, -liftValue);
[UIView animateWithDuration:0.2
animations:^{
self.transform = theTransform;
}
completion:^(BOOL finished){
CGAffineTransform theTransform2 = CGAffineTransformMakeTranslation(0, 0);
[UIView animateWithDuration:0.2
animations:^{
self.transform = theTransform2;
}
completion:^(BOOL finished){
dragState = MKAnnotationViewDragStateNone;
if(!mapView)
[[NSNotificationCenter defaultCenter] postNotificationName:DPAnnotationViewDidFinishDrag object:nil userInfo:[NSDictionary dictionaryWithObject:self.annotation forKey:DPAnnotationViewKey]];
// Added this to select the annotation after dragging.
// This is the behavior for MKPinAnnotationView
if (mapView)
[mapView selectAnnotation:self.annotation animated:YES];
}];
}];
}
else if (newDragState == MKAnnotationViewDragStateCanceling)
{
// drop the pin and set the state to none
CGPoint endPoint = CGPointMake(self.center.x,self.center.y+liftValue);
[UIView animateWithDuration:0.2
animations:^{
self.center = endPoint;
}
completion:^(BOOL finished){
dragState = MKAnnotationViewDragStateNone;
// Added this to select the annotation after canceling.
// This is the behavior for MKPinAnnotationView
if (mapView)
[mapView selectAnnotation:self.annotation animated:YES];
}];
}
}
- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event
{
// When the user touches the view, we need his point so we can calculate by how
// much we should life the annotation, this is so that we don't hide any part of
// the pin when the finger is down.
// Fixed a bug here. If a touch happened while the annotation view was being dragged
// then it screwed up the animation when the annotation was dropped.
if (dragState == MKAnnotationViewDragStateNone)
{
fingerPoint = point;
}
return [super hitTest:point withEvent:event];
}

Setting UIView with animation to default state

I have an animating sine wave in one of my view controllers that appears every time an up swipe gesture is detected and disappears when a down swipe is detected. The sine wave is a UIView and the animation translates the sine wave across the screen. This works fine when I swipe up and down the first time. However, after the first swipe I get all kinds of issues. I noticed that if I reallocate and initialize the sine wave after every swipe down, I can get it to work properly, but I know thats not the correct fix. My assumption is that the frame of the sine wave has to be reset to what it was originally, but my attempt to do so didn't resolve the issue (my attempt is commented out in the code below). Any ideas?
View controller code:
- (void)viewDidLoad
{
[self setUpSine];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(appDidEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
}
-(void) setUpSine
{
self.sineWave = [[OSDrawWave alloc] initWithFrame:CGRectMake(-1*self.view.bounds.size.width, self.view.bounds.size.height, 2*self.view.bounds.size.width, .3125*(2*self.view.bounds.size.width))];
[self.sineWave setBackgroundColor:[UIColor clearColor]];
[self.view insertSubview:self.sineWave belowSubview:self.panedView];
}
- (IBAction)handleUpSwipe:(UISwipeGestureRecognizer *)recognizer
{
[self.sineWave animateWave];
[self showSine];
[UIView animateWithDuration:.25 delay:0 options:UIViewAnimationOptionCurveLinear animations:^
{
self.sineWave.frame = CGRectOffset(self.sineWave.frame, 0, -246);
}
completion:^(BOOL finished)
{
NSLog(#"sine wave y position AFTER UP swipe %f", self.sineWave.frame.origin.y);
}];
}
- (IBAction)handleDownSwipe:(UISwipeGestureRecognizer *)recognizer
{
[self hideSine];
if(self.panedView.frame.origin.y <0)
{
[UIView animateWithDuration:.25 delay:0 options:UIViewAnimationOptionCurveLinear animations:^
{
self.sineWave.frame = CGRectOffset(self.sineWave.frame, 0, 246);
}
completion:^(BOOL finished)
{
}];
}
}
-(void) showSine
{
[self.sineWave setHidden:NO];
[self.sineWave fadeInAnimation];
}
-(void) hideSine
{
[self.sineWave fadeOutAnimation];
[self.sineWave setHidden:YES];
}
-(void) appDidEnterForeground:(NSNotification *)notification
{
[self.sineWave animateWave];
}
My sine wave is a subclass of UIView. This is the code within the sineWave class
- (void)animateWave {
[self.layer removeAllAnimations];
self.transform=CGAffineTransformIdentity;
[UIView animateWithDuration:3 delay:0.0 options: UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction animations:^{
self.transform = CGAffineTransformMakeTranslation(self.frame.size.width/2,0);
} completion:^(BOOL finished) {
self.transform = CGAffineTransformMakeTranslation(-self.frame.size.width/2, 0);
}];
}
-(void) fadeInAnimation
{
[UIView animateWithDuration:0 animations:^{
self.alpha = 1.0;}];
}
-(void) fadeOutAnimation
{
[UIView animateWithDuration:3 animations:^{
self.alpha = 0.0;}];
}
- (void)drawRect:(CGRect)rect
{
self.yc = 30;//The height of a crest.
float w = 0;//starting x value.
float y = rect.size.height;
float width = rect.size.width;
int cycles = 6;//number of waves
self.x = width/cycles;
CGContextRef context = UIGraphicsGetCurrentContext();
CGMutablePathRef path = CGPathCreateMutable();
CGContextSetLineWidth(context, 2);
while (w <= width) {
CGPathMoveToPoint(path, NULL, w,y/2);
CGPathAddQuadCurveToPoint(path, NULL, w+self.x/4, y/2 - self.yc, w+self.x/2, y/2);
CGPathAddQuadCurveToPoint(path, NULL, w+3*self.x/4, y/2 + self.yc, w+self.x, y/2);
w+=self.x;
}
CGContextAddPath(context, path);
[[UIColor colorWithRed:107/255.0f green:212/255.0f blue:231/255.0f alpha:1.0f]setStroke];
CGContextDrawPath(context, kCGPathStroke);
self.alpha= 0.0;
}
I was able to get this working thanks to a clue from this answer
In the view controller you need to subscribe to UIApplicationWillEnterForegroundNotification to get a notification when you app is returning from the background. I changed the viewDidLoad in the view controller and added a method to handle the notification -
- (void)viewDidLoad
{
[super viewDidLoad];
[self setUpSine];
[self.sineWave animateWave];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(appDidEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
}
-(void) appDidEnterForeground:(NSNotification *)notification
{
[self.sineWave animateWave];
}
I also removed the call to animateWave from the handleUpSwipe: method
- (IBAction)handleUpSwipe:(UISwipeGestureRecognizer *)recognizer
{
[self showSine];
[UIView animateWithDuration:.25 delay:0 options:UIViewAnimationOptionCurveLinear animations:^
{
self.sineWave.frame = CGRectOffset(self.sineWave.frame, 0, -246);
}
completion:^(BOOL finished)
{
NSLog(#"sine wave y position AFTER UP swipe %f", self.sineWave.frame.origin.y);
}];
}
You also need to make a change to animateWave to clear any previous animation before the animation is (re)started. You don't need anything in the completion block -
- (void)animateWave {
[self.layer removeAllAnimations];
self.transform=CGAffineTransformIdentity;
[UIView animateWithDuration:3 delay:0.0 options: UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveLinear |UIViewAnimationOptionAllowUserInteraction animations:^{
self.transform = CGAffineTransformMakeTranslation(self.frame.size.width/2,0);
} completion:^(BOOL finished) {
}];
}

rectangles in UIView iOS

I have created a rectangle like this in objective -c :
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
CGContextFillRect(context, CGRectMake(0, 440, 320, 30));
}
in the view controller i have created a function which then calls this rectangle and an animation block so it looks like its coming out by the alpha command :
PopUpRectangle *RectangeView = [[PopUpRectangle alloc] initWithFrame:CGRectMake(0, 538, 320, 30)];
RectangeView.alpha = 0;
[self.view addSubview:RectangeView];
[UIView animateWithDuration:1.5
animations:^ {
RectangeView.alpha = 1;
}];
I have a function that is triggered by an event and then I call the above code to create the rectangle on the screen.
but how do i revert the action, would it be best to create another rectangle which brings the alpha back to 0 ?
thanks
Keep a local reference to RectangeView so you when you want you can animate it back out, and remove it from the view.
[UIView animateWithDuration:1.5
animations:^ {
RectangeView.alpha = 0;
} completion:^(BOOL finished) {
[RectangleView removeFromSuperview];
}];
You could also move your animation block to it's own function.
Like the other comment suggests save a local reference to it so that you can access it later.
- (void)animateBlock{
[UIView animateWithDuration:1.5
animations:^ {
RectangeView.alpha = !RectangleView.alpha; //Reverse the current alpha
} completion:^(BOOL finished) {
[RectangleView removeFromSuperview];
}];
}

how to make smooth UIView splitting animation?

Hi i am trying to implement split the UIView using UIPinchGestureRecognizer, but the following issue are their, if any one know how to solve kindly.
This is my pinchGestureHandlerCode:
- (IBAction)splitView:(UIPinchGestureRecognizer*)recognizer
{
if(recognizer.state == UIGestureRecognizerStateBegan)
{
CGPoint selectedFolderPoint = CGPointMake([recognizer locationInView:recognizer.view].x,
recognizer.view.frame.origin.y);
self.splitPosition = ([recognizer locationOfTouch:0 inView:recognizer.view].x +
[recognizer locationOfTouch:1 inView:recognizer.view].x)/2;
//STEP 1: Capture the Main View Content into an image
[self captureImageFromPointAndSetupMaskView:selectedFolderPoint];
}
else if(recognizer.state == UIGestureRecognizerStateChanged)
{
CGPoint selectedFolderPoint = CGPointMake(recognizer.scale,
recognizer.view.frame.origin.y);
//STEP 2: Reduce the left image width, and move the right image origin according to the finger move of user.
[self layoutBottomPartOfMainViewRelativeToPointInMainView:selectedFolderPoint];
[UIView beginAnimations:#"split" context:NULL];
[UIView setAnimationDuration:0.1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
//captures the main background view's image
//STEP 3- Push The Layer-3 which hosts the other part of the Main Content View
[UIView commitAnimations];
}
else if(recognizer.state == UIGestureRecognizerStateEnded)
{
// STEP 3: remove the both image view from the view.
[self layoutFinalFrameOfBottomPartOfMainContentView];
[UIView commitAnimations];
}
}
STEMP 1 METHOD:
-(void)captureImageFromPointAndSetupMaskView:(CGPoint)selectedFolderPoint
{
UIGraphicsBeginImageContext(self.superview.frame.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *backgroundImage = UIGraphicsGetImageFromCurrentImageContext();
CGImageRef leftTempRef = backgroundImage.CGImage;
CGImageRef leftImageRef = CGImageCreateWithImageInRect(leftTempRef,
CGRectMake(self.bounds.origin.x,
self.bounds.origin.y, selectedFolderPoint.x,
self.bounds.size.height));
UIImage *leftImage = [UIImage imageWithCGImage:leftImageRef];
leftImageBackgroundView = [[UIImageView alloc] initWithImage:leftImage];
[self.leftBackgroundView addSubview:leftImageBackgroundView];
CGImageRef rightTempRef = backgroundImage.CGImage;
CGImageRef rightImageRef = CGImageCreateWithImageInRect(rightTempRef,
CGRectMake(selectedFolderPoint.x,
self.bounds.origin.y, self.superview.bounds.size.width, self.bounds.size.height));
UIImage *rightImage = [UIImage imageWithCGImage:rightImageRef];
rightImageBackgroundView = [[UIImageView alloc] initWithImage:rightImage];
[self.rightBackgroundView addSubview:rightImageBackgroundView];
[self.leftBackgroundView setFrame:CGRectMake(self.bounds.origin.x,
self.bounds.origin.y,
selectedFolderPoint.x,
self.frame.size.height)];
[self.rightBackgroundView setFrame:CGRectMake(selectedFolderPoint.x,
self.bounds.origin.y,
self.superview.bounds.size.width,
self.bounds.size.height)];
}
STEP 2 METHOD:
-(void)layoutBottomPartOfMainViewRelativeToPointInMainView:(CGPoint)selectedFolderPoint
{
[self.leftBackgroundView setFrame:CGRectMake(self.leftBackgroundView.bounds.origin.x,
self.leftBackgroundView.bounds.origin.y,
(self.leftBackgroundView.bounds.size.width -
selectedFolderPoint.x),
self.leftBackgroundView.bounds.size.height)];
[self.rightBackgroundView setFrame:CGRectMake(self.rightBackgroundView.frame.origin.x + selectedFolderPoint.x,
self.rightBackgroundView.bounds.origin.y,
self.rightBackgroundView.bounds.size.width - selectedFolderPoint.x,
self.rightBackgroundView.bounds.size.height)];
if(((ABS(self.leftBackgroundView.bounds.origin.x+self.leftBackgroundView.bounds.size.width)-
self.rightBackgroundView.bounds.origin.x) > 100) &&
((ABS(self.leftBackgroundView.bounds.origin.x+self.leftBackgroundView.bounds.size.width)-
self.rightBackgroundView.bounds.origin.x) < 110))
{
[self switchView];
NSDictionary *message;
[self.timelineController sendEvent:SWITCH_VIEW withMessage:message];
[self layoutFinalFrameOfBottomPartOfMainContentView];
}
[UIView setAnimationsEnabled:NO];
[UIView setAnimationsEnabled:YES];
}
STEP 3 METHOD:
-(void)layoutFinalFrameOfBottomPartOfMainContentView
{
[self.leftBackgroundView setFrame:CGRectMake(self.bounds.origin.x,
self.leftBackgroundView.bounds.origin.y,
self.bounds.origin.x,
self.leftBackgroundView.bounds.size.height)];
[self.rightBackgroundView setFrame:CGRectMake(self.bounds.origin.x,
self.rightBackgroundView.bounds.origin.y,
self.bounds.origin.x,
self.rightBackgroundView.bounds.size.height)];
leftImageBackgroundView.image = nil;
rightImageBackgroundView.image = nil;
}
The issue are:
i am trying to make smooth animation but its running too fast and suddenly moving both side.
if i to same pinch gesture more number of time app getting crash and showing memory warning.
if any one know what mistake i am doing in my code kindly help me to solve the problem or there is any other way to do the same animation kindly give way to me....
Thanks in advance... ...
Define the UIPinchGestureRecognizer like this
UIPinchGestureRecognizer *ges = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(split)];
[self addGestureRecognizer:ges];
and the split Method like this
- (void)split {
CGRect f = self.frame;
CGRect f1 = CGRectMake(CGRectGetMinX(f), f.origin.y, f.size.width/2, f.size.height);
CGRect f2 = CGRectMake(CGRectGetMidX(f), f.origin.y, f.size.width/2, f.size.height);
SplitView *view1 = [[[SplitView alloc] initWithFrame:f1] autorelease];
[self.superview addSubview:view1];
SplitView *view2 = [[[SplitView alloc] initWithFrame:f2] autorelease];
[self.superview addSubview:view2];
f1.origin.x -= 30;
f2.origin.x += 30;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
view1.frame = f1;
view2.frame = f2;
[UIView commitAnimations];
[self removeFromSuperview];
}

Resources