UIView 滑动动画故障
UIView Swipe Animation Glitches
我正在尝试添加滑动以更改标签栏应用程序的视图。点赞浏览量。
到目前为止,我的解决方案是有效的,但我在滑动时遇到了故障,我认为这些是 view.frame (CGRectMake
) 的变化导致了这些黑色故障,但我还没有弄清楚为什么会这样。
它不会发生在像模拟器一样糟糕的设备中,但仍然可见且不流畅。
所以在我的根 tabbar
控制器中 (TabViewController : UITabBarController
)
#import <QuartzCore/QuartzCore.h>
#define XCTabViewDummyNSUintegerNOTargetVC 10
#define XCTabViewSwipeYAxisMax 40
#define XCTabViewSwipeXAxisMax 40
@interface XCTabViewController ()
@property (nonatomic, strong) id<XCTabControlStrategy> tabStrategy;
@property double swipeRatio;
@property double toViewX;
@property double fromViewX;
@property UIView * fromView;
@property UIView * toView;
@end
然后
-(void)viewDidAppear:(BOOL)animated
{
XCDeviceType deviceType = [UIViewUtils currentDeviceType];
swipeRatio = 320;
switch (deviceType) {
case XCiPhone4:
swipeRatio = 320;
break;
case XCiPhone5:
swipeRatio = 320;
break;
case XCiPhone6:
swipeRatio = 375;
break;
case XCiPhone6Plus:
swipeRatio = 414;
break;
default:
swipeRatio = 320;
break;
}
// add pan recognizer to the view when initialized
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panRecognized:)];
[panRecognizer setDelegate:(id)self];
[self.view addGestureRecognizer:panRecognizer]; // add to the view you want to detect swipe on
}
然后
-(void)panRecognized:(UIPanGestureRecognizer *)sender{
CGPoint distance = [sender translationInView: self.view];
if (sender.state == UIGestureRecognizerStateChanged) {
if (distance.x > XCTabViewSwipeXAxisMax && distance.y > -XCTabViewSwipeYAxisMax && distance.y < XCTabViewSwipeYAxisMax) { // right
[sender cancelsTouchesInView];
NSLog(@"user swiped right");
NSLog(@"swipe direction \n ---------------->"); //view on the left should be displayed
NSLog(@"distance.x %f", distance.x);
if (self.selectedIndex >= 1 ) {
NSUInteger targetVc = self.selectedIndex - 1;
// Get the views.
fromView = self.selectedViewController.view;
toView = [[self.viewControllers objectAtIndex:targetVc] view];
// Get the size of the view area.
CGRect viewSize = fromView.frame;
// Add the to view to the tab bar view.
[fromView.superview addSubview:toView];
// Position it off screen.
toView.frame = CGRectMake(-swipeRatio, viewSize.origin.y, swipeRatio, viewSize.size.height);
[UIView animateWithDuration:0 animations:^{
// Animate the views on and off the screen. This will appear to slide.
fromView.frame =CGRectMake(distance.x, viewSize.origin.y, swipeRatio, viewSize.size.height);
toView.frame =CGRectMake(-swipeRatio+distance.x, viewSize.origin.y, swipeRatio, viewSize.size.height);
toViewX = -swipeRatio+distance.x;
fromViewX = distance.x;
}];
}
}
}
如何让这个动画更流畅?
同样在剧烈滑动时我收到内存警告。
原来问题出在这一行
// Position it off screen.
toView.frame = CGRectMake(-swipeRatio, viewSize.origin.y, swipeRatio, viewSize.size.height);
每次 distance.x 更改时将视图定位到屏幕外是导致故障的原因
这是我的完整解决方案
NSLog(@"user swiped right");
NSLog(@"swipe direction \n ---------------->"); //view on the left should be displayed
NSLog(@"distance.x %f", distance.x);
if (self.selectedIndex >= 1 ) {
NSUInteger targetVc = self.selectedIndex - 1;
// Get the views.
if (!fromView) {
fromView = [[UIView alloc] init];
fromView = self.selectedViewController.view;
}
// Get the size of the view area.
CGRect viewSize = fromView.frame;
if (!toView) {
toView = [[UIView alloc] init];
toView = [[self.viewControllers objectAtIndex:targetVc] view];
// Position it off screen.
toView.frame = CGRectMake(swipeRatio, viewSize.origin.y, swipeRatio, viewSize.size.height);
// Add the to view to the tab bar view.
[fromView.superview addSubview:toView];
}
[UIView animateWithDuration:0 animations:^{
// Animate the views on and off the screen. This will appear to slide.
fromView.frame = CGRectMake(distance.x, viewSize.origin.y, swipeRatio, viewSize.size.height);
toView.frame = CGRectMake(-swipeRatio+distance.x, viewSize.origin.y, swipeRatio, viewSize.size.height);
toViewX = -swipeRatio+distance.x;
fromViewX = distance.x;
}];
我正在尝试添加滑动以更改标签栏应用程序的视图。点赞浏览量。
到目前为止,我的解决方案是有效的,但我在滑动时遇到了故障,我认为这些是 view.frame (CGRectMake
) 的变化导致了这些黑色故障,但我还没有弄清楚为什么会这样。
它不会发生在像模拟器一样糟糕的设备中,但仍然可见且不流畅。
所以在我的根 tabbar
控制器中 (TabViewController : UITabBarController
)
#import <QuartzCore/QuartzCore.h>
#define XCTabViewDummyNSUintegerNOTargetVC 10
#define XCTabViewSwipeYAxisMax 40
#define XCTabViewSwipeXAxisMax 40
@interface XCTabViewController ()
@property (nonatomic, strong) id<XCTabControlStrategy> tabStrategy;
@property double swipeRatio;
@property double toViewX;
@property double fromViewX;
@property UIView * fromView;
@property UIView * toView;
@end
然后
-(void)viewDidAppear:(BOOL)animated
{
XCDeviceType deviceType = [UIViewUtils currentDeviceType];
swipeRatio = 320;
switch (deviceType) {
case XCiPhone4:
swipeRatio = 320;
break;
case XCiPhone5:
swipeRatio = 320;
break;
case XCiPhone6:
swipeRatio = 375;
break;
case XCiPhone6Plus:
swipeRatio = 414;
break;
default:
swipeRatio = 320;
break;
}
// add pan recognizer to the view when initialized
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panRecognized:)];
[panRecognizer setDelegate:(id)self];
[self.view addGestureRecognizer:panRecognizer]; // add to the view you want to detect swipe on
}
然后
-(void)panRecognized:(UIPanGestureRecognizer *)sender{
CGPoint distance = [sender translationInView: self.view];
if (sender.state == UIGestureRecognizerStateChanged) {
if (distance.x > XCTabViewSwipeXAxisMax && distance.y > -XCTabViewSwipeYAxisMax && distance.y < XCTabViewSwipeYAxisMax) { // right
[sender cancelsTouchesInView];
NSLog(@"user swiped right");
NSLog(@"swipe direction \n ---------------->"); //view on the left should be displayed
NSLog(@"distance.x %f", distance.x);
if (self.selectedIndex >= 1 ) {
NSUInteger targetVc = self.selectedIndex - 1;
// Get the views.
fromView = self.selectedViewController.view;
toView = [[self.viewControllers objectAtIndex:targetVc] view];
// Get the size of the view area.
CGRect viewSize = fromView.frame;
// Add the to view to the tab bar view.
[fromView.superview addSubview:toView];
// Position it off screen.
toView.frame = CGRectMake(-swipeRatio, viewSize.origin.y, swipeRatio, viewSize.size.height);
[UIView animateWithDuration:0 animations:^{
// Animate the views on and off the screen. This will appear to slide.
fromView.frame =CGRectMake(distance.x, viewSize.origin.y, swipeRatio, viewSize.size.height);
toView.frame =CGRectMake(-swipeRatio+distance.x, viewSize.origin.y, swipeRatio, viewSize.size.height);
toViewX = -swipeRatio+distance.x;
fromViewX = distance.x;
}];
}
}
}
如何让这个动画更流畅?
同样在剧烈滑动时我收到内存警告。
原来问题出在这一行
// Position it off screen.
toView.frame = CGRectMake(-swipeRatio, viewSize.origin.y, swipeRatio, viewSize.size.height);
每次 distance.x 更改时将视图定位到屏幕外是导致故障的原因
这是我的完整解决方案
NSLog(@"user swiped right");
NSLog(@"swipe direction \n ---------------->"); //view on the left should be displayed
NSLog(@"distance.x %f", distance.x);
if (self.selectedIndex >= 1 ) {
NSUInteger targetVc = self.selectedIndex - 1;
// Get the views.
if (!fromView) {
fromView = [[UIView alloc] init];
fromView = self.selectedViewController.view;
}
// Get the size of the view area.
CGRect viewSize = fromView.frame;
if (!toView) {
toView = [[UIView alloc] init];
toView = [[self.viewControllers objectAtIndex:targetVc] view];
// Position it off screen.
toView.frame = CGRectMake(swipeRatio, viewSize.origin.y, swipeRatio, viewSize.size.height);
// Add the to view to the tab bar view.
[fromView.superview addSubview:toView];
}
[UIView animateWithDuration:0 animations:^{
// Animate the views on and off the screen. This will appear to slide.
fromView.frame = CGRectMake(distance.x, viewSize.origin.y, swipeRatio, viewSize.size.height);
toView.frame = CGRectMake(-swipeRatio+distance.x, viewSize.origin.y, swipeRatio, viewSize.size.height);
toViewX = -swipeRatio+distance.x;
fromViewX = distance.x;
}];